0

I want to allow the user to put some text after a place holder via a printf like:

Text: 

And then I want to be able to count the letters in the string.

int main(void)
{
string text = get_string("Text ");
int num_letters;


 for (i = 0, len = strlen(text); i < num_letters; i++)
 {
   printf("%s\n %i letters", text, num_letters); 
   
 }

}

2 Answers 2

1

The code doesn't work because num_letters is 0. The for loop will never run. In fact, it won't compile either because i and len are not created/defined before attempting to use them.

0

You should loop till \0 (null-terminating) character I.e end of the string and increase the count of num_letters

You have to store user input into a string through get_string

string text = get_string();

Now you can check and loop till end of the string by

text[i] != '\0' 

(where I is the int count inside for loop)

You can increase the letter count as.

num_letters++;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .