Thread: Simple problem, yet difficult for n00b like me :-/

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    17

    Question Simple problem, yet difficult for n00b like me :-/

    I've been browsing the boards for sometime, so I've decided to post.

    I found - somewhat of an answer, although I'm still having problems.

    Simply put - I'm trying to convert the string to upper case. The first letter in each word to be exact.

    ex: "this is my special string"
    converted to: "This Is My Special String"

    I've been trying to work with strcmp( ), but to no avail...
    -----

    I started off by using:

    Code:
    char myChar[] = "this is my test string";
    
    for (i = 0; i < sizeof(myChar); i++)
    {
            // find space, next character change into uppercase
            if ( !strcmp(myChar[i], " ") )
            {
                    myChar[i+1] = toupper(myChar[i+1]);
            }
    }
    -----
    From there I received error C2664: 'strcmp' : cannot convert parameter 1 from 'const char' to 'const char *'
    So I tried..
    ------
    Code:
    const char *myChar = "this is my test string"; // removed [], due to error
    
    for (i = 0; i < sizeof(myChar); i++)
    {
            // find space, next character change into uppercase
            if ( !strcmp(myChar[i], " ") )
            {
                    myChar[i+1] = toupper(myChar[i+1]);
            }
    }
    ------
    This still gives me the same error! I can't seem to figure out why... I've been working on it for a couple of hours too!

    Any insight would be appreciated :)

  2. #2
    Unregistered
    Guest
    look at toupper function

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    Well the problem lies in the strcmp function...and the different char formats..

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    2

    Exclamation Hmm

    My first time posting hope i can help umm

    First thing i wonder is did you put

    #include<strings.h>

    along with your code.
    You didn't tell us the include statements this is what your first prob might be.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    317
    Here you go, had to change your logic a bit.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(){
    	char myChar[] = "this is my test string";
    
    	myChar[0] = toupper(myChar[0]); //1st letter to upper
    	for (int i = 0; i < sizeof(myChar); i++)
    	{       
    		// find space, next character change into uppercase
            if ( myChar[i]== char(32) ) /*space is int 32 so type casted to char, also since single char used '==' instead of strcmp*/
            {
                    myChar[i+1] = toupper(myChar[i+1]);
            }
    	}
        cout<<myChar;
    	cin.get();
    	return(0);
    }
    That works, hope I helped.

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Different Way..

    Code:
    #include <iostream.h>
    
    int main()
    {
      const int MAX_BUFFER=512;
      char Buffer[MAX_BUFFER];
    
      int i=0, StringLen;
    
      cout << "Please enter string to be capitalised: ";
      cin.getline(Buffer, StringLen=sizeof(Buffer));
    
      Buffer[0]=toupper(Buffer[0]);
    
      for(i; i < StringLen; i++){
        if(Buffer[i]==' ')
          Buffer[i+1]=toupper(Buffer[i+1]);
      }
    
      cout << "Converted string: " << Buffer;
      getchar();
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    ahh, thanks you guys

    I can't tell you how frustrated that has made me!!

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    And another way:
    Code:
    #include <string.h>
    #include <iostream.h>
    #include <ctype.h>
    
    int main()
    {
      char myChar[] = "this is my test string";
      char *ptr = myChar;
    
      for(*ptr = toupper(*ptr); (ptr = strchr(ptr, ' ')) != NULL; ptr++, *(ptr) = toupper(*(ptr))) ;
      cout << myChar;
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  3. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 11:34 PM
  4. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 04:07 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM