Thread: Need help with capitalizing char strings

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

    Need help with capitalizing char strings

    Hi all,
    I'm taking an intro course to C and am starting to get very lost. We're on the topic of character strings and are being asked to create a program that will capitalize the first letter of every word in the provided prose. I'm successful in getting the entire thing capitalized but can't figure out how to make it so I can access the first characters of each word. Below is the code that capitalizes all the letters. Can someone provide guidance as to how I'd need to make it so it capitalizes only the first letters?
    The output should read, To Be Or Not To Be That Is The Question.

    Thank you in advance. Below is my code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main(void)
    {
      char prose[] = "to be or not to be that is the question";
      int  i, len;
      
      puts (prose);
      
      len = strlen(prose);
    
      for (i = 0; i < len; i++)
      {
        prose[i] = toupper(prose[i]);
      }
    
      puts (prose);
      
      return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Can someone provide guidance as to how I'd need to make it so it capitalizes only the first letters?
    It means capitalize only first letter of the string and each other letter that follows the space?

    you can check for the space with isspace function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    yes

    yes, that's correct, so each letter after a space would be capitalized. Thanks for the isspace tip

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Hey there, i found it in my directory. Check it out this might help you!

    Code:
    #include <stdio.h>
    
    int main(void)
    {
     char s[100];
     int flag=1;
     printf("\n\nEnter Any String :");
     gets(s);
     for(int i=0;s[i]!='\0';i++)
     {
    	if(s[i]==' ')
    	{
    		flag=1;
    		i++;
    	}
    	if (flag==1 && s[i]>=97 && s[i]<=122)
    		s[i]=s[i]-32;
    	flag=0;
     }
     printf(s);
     return 0;
    }
    Last edited by chottachatri; 03-22-2008 at 11:55 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    do not use gets and magic constants
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    causes error

    Thanks for the code. I tried it but it comes up with the following error:
    fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
    Build log was saved at "file://c:\Documents and Settings\Owner\Desktop\test\test\Debug\BuildLog.ht m"

    Do you know how I can tweak it so I don't get the error?

    Thanks.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Question

    Quote Originally Posted by vart View Post
    do not use gets and magic constants
    WHy So??

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Hey sorry that was the code in C++! oops i've corrected it!you can now try it!

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by chottachatri View Post
    WHy So??
    About gets - read FAQ
    about avoiding magic constants - read the OP code and learn how to use toUpper function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Hey chottachatri,

    I keep getting an error related to 'i', saying it's an undeclared identifier.
    do you know a way around this? I declared int i in the beginning but still didn't work.

  11. #11
    Registered User sndpchikane's Avatar
    Join Date
    Mar 2008
    Posts
    5

    Question

    Hey ChotaChatri!!

    Are you from India?

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    chottachatri,
    Nevermind, I put int i in the wrong place...Works like a charm!!!

    Thanks for the help my friend.
    Joe

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by JoelearningC View Post
    Hey chottachatri,

    I keep getting an error related to 'i', saying it's an undeclared identifier.
    do you know a way around this? I declared int i in the beginning but still didn't work.
    do not use his code - use the idea with the flag and modify your own code. use isspace function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Oh god!! ok fine..i didn't modify it properly..you can declare i in the beginning That rules of Cpp oops..sorry for inconvience..let me post the new code with all bugs removed

    Code:
    #include <stdio.h>
    
    int main(void)
    {
     char s[100];
     int flag=1,i;
     printf("\n\nEnter Any String :");
     fgets(s,99,stdin);
     for(i=0;s[i]!='\0';i++)
     {
    	if(s[i]==' ')
    	{
    		flag=1;
    		i++;
    	}
    	if (flag==1 && s[i]>=97 && s[i]<=122)
    		s[i]=s[i]-32;
    	flag=0;
     }
     printf(s);
     return 0;
    }
    Please let me know if this is working or not!

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Quote Originally Posted by sndpchikane View Post
    Hey ChotaChatri!!

    Are you from India?
    Yup from India studying in 2nd year
    Why?
    Last edited by chottachatri; 03-23-2008 at 12:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. help with strings
    By webbizdesign in forum C Programming
    Replies: 5
    Last Post: 11-13-2003, 08:20 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM