Why don’t you want to step up?

Asked

Viewed 113 times

-3

I want to increment the cont variable if the character vector is not null, do you understand? But nothing happens. Why?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void main(){

int i;
char testeNome[190];
int cont;
printf("Digite seu primeiro nome: ");
scanf("%s",testeNome);


while(testeNome != "\0"){

    cont++; 
    }

printf("%d",cont);
}
  • Ps.: I cannot use strlen so I want to find out the vector size value

1 answer

4


Girlfriend, follow the code working:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main(){

int i = 0;
char testeNome[190];
int cont = 0;
printf("Digite seu primeiro nome: ");
scanf("%s",testeNome);

while(testeNome[i] != '\0'){
    cont++;
    i++;
}

printf("%d",cont);
}

you forgot to use the variable i to treat the characters of the string, using it in testName and incrementing i inside the loop. Also, since 0 is a character, we need to indicate it by simple quotation marks.

Aah, and it’s always good to start the variables (cont and i) with 0 to avoid having some value from a previous execution.

I hope I’ve helped

  • Friend* am woman = P

  • Oops! Force of habit, I apologize rsrs

  • rs =]. Relax. Thanks for helping. =]

Browser other questions tagged

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