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

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    15

    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. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by engstudent363 View Post
    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. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    23
    the best code is strlen(variable)
    and
    printf(the variable)..

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    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.
    Last edited by epboyd; 04-08-2008 at 06:06 AM. Reason: misspelled stdio

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-23-2005, 10:51 AM
  2. preventing user entering characters
    By Ashkan in forum C Programming
    Replies: 12
    Last Post: 08-24-2003, 12:56 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. check individual characters on input
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 09:56 PM
  5. limit the characters using gets function
    By yukon in forum C Programming
    Replies: 6
    Last Post: 10-08-2001, 02:15 PM