Thread: help simple string outputting

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    help simple string outputting

    Can anyone help me, we have this assignment. A program that will ask for the full name (first name and last name) and the birthday. then the program will output the first letter of full name and last name in capital.

    ex.

    Input name: dante lopez
    Your name is Dante Lopez

    I have a problem in making the first letter of the last name into upper case.

    Here is my C code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    
    void main()
    {
    
    int x=0, mm,dd,yyyy,ctr,i,count;
    char name[25],choice;
    clrscr();
    
    puts("Input your name: ");
    gets(name);
    count=strlen(name);
    
    //name[25]=tolower(name[25]);
    name[0]=toupper(name[0]);
    
    for(x=0;name[x]<=count;x++)
    {
    i = isspace (name[x]);
    if(i==1)
    name[x++]=toupper(name[x++]);
    }
    
    printf("Input your birthday [mm/dd/yyyy]: ");
    scanf("%d/%d/%d",&mm,&dd,&yyyy);
    printf("\n\n");
    puts(name);
    
    if(mm==01)
    printf("January %d %d",dd,yyyy);
    else if(mm==02)
    printf("February %d %d",dd,yyyy);
    else if(mm==03)
    printf("March %d %d",dd,yyyy);
    else if(mm==04)
    printf("April %d %d",dd,yyyy);
    else if(mm==05)
    printf("May %d %d",dd,yyyy);
    else if(mm==06)
    printf("June %d %d",dd,yyyy);
    else if(mm==07)
    printf("July %d %d",dd,yyyy);
    else if(mm== 8)
    printf("August %d %d",dd,yyyy);
    else if(mm== 9)
    printf("September %d %d",dd,yyyy);
    else if(mm==10)
    printf("October %d %d",dd,yyyy);
    else if(mm==11)
    printf("November %d %d",dd,yyyy);
    else if(mm==12)
    printf("December %d %d",dd,yyyy);
    else
    printf("Month should be 1-12");
    
    printf("\n\nWould still like to input [y/n]: ");
    scanf("%s",&choice);
    
    getch();
    }
    Hope someone can help me...

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, what will you always have to help mark the second name?

    How about a space - as in the space between the first and second name. And we know that the first letter after the space, will be the very letter that you want to capitalize.

    So, use strchr() to find the first space, and then increment the pointer one time, and then capitalize that letter with toupper().

    Or, you can scan through the name, from 0 to the index whose element in the array holds the space value (' '), or 32 - either one it's the same.

    So whichever way you want to do it, it will be done in approximately the same way.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    @Adak

    thanks for the reply. Yes the space is the identifier for the last name. how will i use the strchr(), where will i put it? sorry i'm only new to this C language.

    Thanks.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you're new than you want to do it yourself, just to get the experience:

    in a for loop, let's walk through the char array:

    Code:
    for(i = 0; yourArray[i] != 32; i++);
    
    //Note the semi-colon on the end - this is generally *wrong*, but this for loop needs
    //to do nothing else, for us.
    Now yourArray[i] is your space between names, so capitalize the yourArray[i+1], and you're on to the next problem.

    Because of differences in compilers and computers, yourArray[i] may have gone one too far, and already be at the first letter of the last name. Just FYI.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    I can't seem to understand the strchr(). I'm having trouble in this string manipulation, can you please fix my loop part in my code.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    @Adak

    I've changed my loop part into this:
    Code:
    for(x=0;name[x]!=' ';x++)
    {
    if(name[x]==' ')
    name[x+1]=toupper(name[x+1]);
    }
    Still no luck

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Can somebody help me please....

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by kingdm View Post
    @Adak

    I've changed my loop part into this:
    Code:
    for(x=0;name[x]!=' ';x++)
    {
    if(name[x]==' ')
    name[x+1]=toupper(name[x+1]);
    }
    Still no luck
    You can't change the for loop logic around, and say "no luck". I gave you the logic, and the code - just use it, as I gave it to you.

    Here's an example, in a program.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void) {
      char name[25] = "samuel adams";
      int i;
    
      for(i = 0; name[i] != 32; i++);
    
      name[0] = toupper(name[0]);
      name[i] = toupper(name[++i]);
      
      printf("\n\n %s", name);
    
      printf("\n\n\t\t\t     press enter when ready");
      i = getchar();
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Code:
    /* Get a pointer to the location of the space character */
    char *ptr = strchr(name, ' ');
    
    /* Make sure you actually found a space character */
    if (ptr != NULL)
       ptr[1] = toupper(ptr[1]);
    Keep in mind that this
    Code:
    for(i = 0; name[i] != 32; i++);
    is dangerous if the input string doesn't have a space character in it.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Then you could add:

    Code:
    for(i = 0; name[i] != ' ' && i < sizeof(name); i++);
    Assuming name is an array of chars, if the array size is defined as a constant sizeof() could be replaced with that.
    Last edited by Subsonics; 11-08-2009 at 01:04 PM.

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Thanks a lot to all who replied, got the program working. Adak thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Simple problem with string... Please take a look
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-03-2006, 06:48 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM