# Thread: How do you check how many characters a user has entered?

1. ## How do you check how many characters a user has entered?

Lets say I have the code below. This program example would ask the user to enter a string of characters, then save those characters into an array named "s1". How can I find out how many characters they have entered because as an example they could enter "hello" which would be 5 characters or "what" which would be 4 characters. Don't know how to quantify the amount of characters that they've entered though. I would like to know how to do this in case the user enters more characters than the size of the array I would like to be able to print out an error message and then prompt them to enter a new string of characters.

Code:
```printf("Enter a string of characters:\n");
scanf("%s", s1);```

2. Originally Posted by engstudent363
Lets say I have the code below. This program example would ask the user to enter a string of characters, then save those characters into an array named "s1". How can I find out how many characters they have entered because as an example they could enter "hello" which would be 5 characters or "what" which would be 4 characters. Don't know how to quantify the amount of characters that they've entered though. I would like to know how to do this in case the user enters more characters than the size of the array I would like to be able to print out an error message and then prompt them to enter a new string of characters.

Code:
```printf("Enter a string of characters:\n");
scanf("%s", s1);```
If the user enters more characters than you have room for in your array, it's too late: you can't detect that because you've entered the realm of undefined behavior--that essentially means anything can happen and you have no guarantees about the state of your program.

Generally speaking, fgets() is the preferred function for reading a string of text from the user. It has bounds checking, so if you call it properly, your array won't be overrun. You can generally detect whether the user entered too many characters by seeing if a newline (that is, a '\n' character) is present in the string. If it is, everything the user typed was read. If there is no newline, odds are that not everything he typed was read. Of course... as you will come to learn in C, that's not the whole story, and reading user input is really one of the annoying aspects of C.

For beginners, it's probably best to make your array "large enough", whatever you determine that to be, and hope the user doesn't write a novel when you give him the opportunity to type something.

3. I too would like to recommend fgets() instead of scanf().

But to answer your actual question (aside from "when the user enters too many chatracters" - because with scanf(), it is, as pointed out, too late AFTER the scanf() is finished - your application will most likely have crashed at that point): use strlen() to count the length of the string.

--
Mats

4. the best code is strlen(variable)
and
printf(the variable)..

5. Code:
```#include <stdio.h>

int main(void) {
char string[255];
printf("Enter a string : ");
fgets(string, 255, stdin);
printf("\nYou have entered &#37;d characters",strlen(string));
system("pause");```
Hope that helps.

Popular pages Recent additions