turbo c

Hi, Can anyone point me to a post or tutorial that would help with answering this problem? No answers needed -- just pointers that would help point to the right direction to solving this. Thanks.


Prompt the user for full name. Keep on asking until all characters are valid. Then display full name with first letters capital.
Result should be something like this:


Enter your name: juan dela 123 cruz...
Name has invalid characters!
Enter your name: jUan dEla CrUx...
Your name is: Juan Dela Cruz

Comments

  • After taking the input from the user, you need a for loop and the functions toupper() and tolower() from ctype.h.
  • : After taking the input from the user, you need a for loop and the
    : functions toupper() and tolower() from ctype.h.

    Thank you. I got that idea but can you point me to a forum or tutorial that shows how to use them and gives an outline of the structure? Sorry to bother you with this trivial problem -- I've just been trying to find a tutorial that covers this.
  • Thank you very much!
  • Where can I search for forums about this?

  • The easy and perhaps best way is to loop through the string and manually check every letter.

    The hard, fancy and not particulary efficient way is to use the strtok() function (string tokenizer). I'd stay away from that one if you are new to string handling.
  • Thank you again. You have to forgive an this amateur who's trying to learn the ropes.

  • main()
    {
    int x;
    char name[30];

    clrscr();

    puts("enter your name: ");
    gets(name);

    for(x=0;x<strlen(name);x++)
    {
    if(isalpha(name[x])==0 && isspace(name[x])==0)
    {
    puts("Name contains invalid character!
    Enter name: ");
    gets(name);
    }

    }

    printf("
    Your name is:
    ");
    printf("%c", toupper(name[0]));

    for(x=1; x<strlen(name); x++)
    {
    if(isupper(name[x])!=0)
    {
    printf("%c", tolower(name[x]));
    }
    else if(isspace(name[x])!=0)
    {
    printf(" %c", toupper(name[x+1]));
    x+=1;
    }
    else
    {
    printf("%c", name[x]);
    }
    }

    getch();
    }
  • Some comments:


    : main()

    This isn't valid C. It should be int main().


    : puts("enter your name: ");
    : gets(name);

    This will only read the string until it encounters a space. That is, it will only read the forename. fgets() might be a better function if you wish to read several strings and store them in one array.


    : for(x=0;x<strlen(name);x++)

    A hint for optimizing: never use strlen() inside the loop condition, or it will be executed for every run of the loop. Your program will be much faster if you write like this:

    int length;
    ...

    length = strlen(name);
    for(x=0; x<length; x++)


    : if(isalpha(name[x])==0 && isspace(name[x])==0)

    This should work, but note that the names will contain spaces.


    : else if(isspace(name[x])!=0)
    : {
    : printf(" %c", toupper(name[x+1]));
    : x+=1;

    This will likely cause trouble. If you run into a space, you will want to print the space. Otherwise the names will appear like "BillGates".


    Also, avoid clrscr() and getch() when possible, since they aren't standard C functions.
  • That was great! Thank you very much for your input.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories