Char count in C

Asked

Viewed 132 times

2

I have a C function that should take a string, and pointers that have the amount of numbers and vowels that are present in the string, but the function is not adding up each time it finds a number or a vowel. Follows my code:

int main(){
    char string[12]={"5bana22anab5"};
    bool palindromo = true;
    int qtdnumero=0,qtdvogal=0,qtdoutros=0;
    informacoes(string,&palindromo,&qtdnumero,&qtdvogal,&qtdoutros);
    if(palindromo == true){
        printf("true ");
    }else printf("false ");
    printf("numeros: %d vogais: %d outros: %d",qtdnumero,qtdvogal,qtdoutros);

}

void informacoes(char str[12], bool *palindromo, int *qtdnumero, int *qtdvogal, int *qtdoutros){
    int i, j=11,igual=1;
    for(i=0;i<6;i++){
       if(str[i]!=str[j]){
          igual=0;
       }
       if(str[i]=='0' || str[i]=='1' || str[i]=='2' || str[i]=='3' || str[i]=='4' || str[i]=='5' || str[i]=='6' || str[i]=='7' || str[i]=='8' || str[i]=='9')
          *qtdnumero++;
       else if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u'){
          *qtdvogal++; 
       }
       else
          *qtdoutros++;

       if(str[j]=='0' || str[j]=='1' || str[j]=='2' || str[j]=='3' || str[j]=='4' || str[j]=='5' || str[j]=='6' || str[j]=='7' || str[j]=='8' || str[j]=='9')
          *qtdnumero++;
       else if(str[j]=='a' || str[j]=='e' || str[j]=='i' || str[j]=='o' || str[j]=='u'){
          *qtdvogal++; 
       }
       else
          *qtdoutros++;
       j--;
    }
    if (igual==0){
        *palindromo=false;
    }
}

The result of the function is = true numbers: 0 vowels: 0 other: 0 Does anyone have any idea why the function is not increasing the counters?

  • #include <ctype.h> and replaces those ifs huge for if (isdigit((unsigned char)str[j])) /* ... */;

1 answer

3


The instruction *ponteiro++ (*(ponteiro++)) increases the pointer and returns the existing value before increment.
To increase the pointed value uses (*ponteiro)++ (the value returned is also the value pointed before the increment).

int a[2] = {42, -1};
int *ponteiro = a;
(*ponteiro)++; // a[0] += 1;
*ponteiro++;   // ponteiro aponta para a[1]; o valor da expressao e 43
43;            // expressao com valor 43, sem efeitos colaterais
11*4-1;        // outra expressao de valor 43
(*ponteiro)++; // a[1] += 1;
printf("%d %d\n", a[0], a[1]);

Browser other questions tagged

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