By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

String Length Validation

DanielTNBaker
P: 7
Basically i would like to know how to validate the length of a string

Expand|Select|Wrap|Line Numbers
  1. printf("\n\n\n\n\t Please Enter the Name of the Client: ");
  2.    fflush(stdin);
  3.    gets(client.name);
  4.    do
  5.    {
  6.        if ()
  7.        {
  8.                            printf("ERROR - Length of Name to Long")
  9.                            printf("\n\n\n\n\t Please Enter the Name of the Client: ");
  10.                            fflush(stdin);
  11.                            gets(client.name);
  12.        }
  13.    }while ();
this is what i got but im not sure where to go from here basically i want to say pretty much if the name is more than 30 characters print an error message and then repeat the prompt.

if anyone can help would be much appreciated
Mar 16 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
I assume client.name is a char[] of char * pointing to a char array.
You can use the strlen() function to determine how many characters are in the string
http://www.cplusplus.com/reference/clibrary/cstring/

make sure that your array is sufficently long to take the worst case - it is probably better to use fgets() where you specify the maximum number of characters that can be read
http://www.cplusplus.com/reference/clibrary/cstdio/fgets.html

note that fgets() leaves the \n newline character in the string (gets() does not)
Mar 16 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.