Scan string in multiple of 3

Asked

Viewed 61 times

0

People how to scan a string every 3 characters?

I was doing like this

for(x=0;str_P[x];x++){
 if (str_P[x] == 'ABC'||'abc'){ printf("1,");
 }if(str_P[x] == 'CBA'||'cba'){ printf("2,");
 }if(str_P[x] == 'BCA'||'bca'){ printf("3,");
 }if(str_P[x] == 'ACB'||'acb'){ printf("4,");
 }if(str_P[x] == 'CAB'||'cab'){ printf("5,");
 }if(str_P[x] == 'BAC'||'bac'){ printf("6,");
 }

But I got this mistake

multi-character character constant [-Werror,-Wmultichar]
             if(str_P[x] == 'ABC'||'abc'){ printf("1,");
                            ^
multi-character character constant [-Werror,-Wmultichar]
             if(str_P[x] == 'ABC'||'abc'){ printf("1,");
                                   ^
error: use of logical '||' with constant operand
  [-Werror,-Wconstant-logical-operand]
             if(str_P[x] == 'ABC'||'abc'){ printf("1,");
                                 ^ ~~~~~
note: use '|' for a bitwise operation
             if(str_P[x] == 'ABC'||'abc'){ printf("1,");
                                 ^~~~~~~
                                 |
  • Formatting both the question with answer is done with Markdown. Read Help with Markdown

  • The error is trying to make two simultaneous comparisons using the operator || in str_P[x] == 'ABC'||'abc'. You do something like this:(str_P[x] == 'ABC') || (str_P[x] == 'abc') in all expressions.

  • https://i.stack.Imgur.com/7g02T.png

  • if ((str_P[x] == 'ABC') || (str_P[x] == 'abc')){}

  • C pira that tmb tried so! (https://i.stack.Imgur.com/D0azg.png)

1 answer

2

There are several things that are not right and need to review and correct.

  1. C strings are delimited with double quotes and not single quotes. Single quotes are reserved for characters only. Example:

    char texto[15] = "texto"; //uma string para 14 letras
    char letra = 'a'; //uma caratere apenas
    
  2. Comparison of strings in C is done with the function strcmp and not with == as he did

  3. The combination of several conditions with && or || must always have two operands in each comparison. Soon (x == 3 || 4) is not correct and has to be (x == 3 || x == 4).

  4. When does str_P[x] is accessing only one letter, so there’s no point in comparing it to ABC. A letter can never have 3 letter text.

  5. To walk 3 in 3 letters, just change the increment of the for. Instead of doing x++ can do x += 3. Alternatively you can test whether it goes in a multipla letter of 3 with the rest operator: x % 3 == 0

Correcting all this and trying to apply logic, keeping as close as possible could do so:

for (x = 0; ; x++){

    if (x > 0 && x % 3 == 0){
        char ultimas3[] = { str[x - 3], str[x - 2], str[x - 1], '\0' };

        if (strcmp(ultimas3, "ABC") ==  0 || strcmp(ultimas3, "abc") == 0){
            printf("\n1,");
        }
        else if (strcmp(ultimas3, "CBA") ==  0 || strcmp(ultimas3, "cba") == 0){
            printf("\n2,");
        }
        else if (strcmp(ultimas3, "BCA") ==  0 || strcmp(ultimas3, "bca") == 0){
            printf("\n3,");
        }
        else if (strcmp(ultimas3, "ACB") ==  0 || strcmp(ultimas3, "acb") == 0){
            printf("\n4,");
        }
        else if (strcmp(ultimas3, "CAB") ==  0 || strcmp(ultimas3, "cab") == 0){
            printf("\n5,");
        }
        else if (strcmp(ultimas3, "BAC") ==  0 || strcmp(ultimas3, "bac") == 0){
            printf("\n6,");
        }
    }

    if (str[x] == '\0'){
        break;
    }
}

In this example I construct a new string with the last 3 letters and a terminator, to then compare with the predefined texts using strcmp. Note that I do not advance by 3 letters, as you can pass the terminator '\0' if the input string does not have a multiple size of 3, which generates undefined behavior and potential Segmentation fault.

See this example in Ideone

  • Shooow! Fiufiiu.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.