Thread: my upit() using toupper() in cctype.h

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    my upit() using toupper() in cctype.h

    cant get my upit() function to work! cant find anything wrong.
    Code:
    #include<iostream>
    #include<cctype>
    #include<cstring>
    using namespace std;
    
    class String		//user-defined string type
    {
    private:
    	char* str;	//pointer to string
    public:
    	String(char* s)
    	{
    		int length = strlen(s);	//length of string argument
    		str = new char[length+1];	//get memory
    		strcpy(str,s);	//copy string
    	}
    
    	~String()
    	{
    		cout << "Deleting str.\n";
    		delete[] str;
    	}
    
    	void upit()	//convert to uppercase if necessary
    	{
    		int length = strlen(str);
    		for(int i = 0; i <= length; i++)
    			toupper(*str+i);
    
    		//int i = 0;
    		//while( *(str+i) != '\0' )
    		//{
    		//	toupper(*(str+i));
    		//	i++;
    		//}
    	}
    
    	void display()	//display the string
    	{
    		cout << str << endl;
    	}
    };
    ive made 2 attempts in the upit() both of them dont work. can anyone give me a kick in the face to the right direction? thanks in advance.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Assign the converted value back to the string.
    Code:
       void upit()    //convert to uppercase if necessary
       {
          int length = strlen(str);
          for ( int i = 0; i < length; i++ )
             str[i] = toupper(str[i]);
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well, one way to do it:
    Code:
    #include <iostream>
    #include <string.h>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main() {
    
    	char myString[] = "this is a test";
    	char* pString;
    
    	cout << "The message before convert: " << myString << endl;
    	
    	for(pString = myString; pString < myString + strlen(myString); pString++){
    		*pString = toupper(*pString);
    	}
    
    	cout << "The message after convert: " << myString << endl;
    
    	cin.get();
    	return 0;
    }
    Dammit, beaten!!
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    lol thanks guys. didnt read carefully. toupper returns a character that has been converted. DAMMIT! details xion...details!~!~~!

    Code:
    void upit()	//convert to uppercase if necessary
    	{
    		for(int i = 0; i <= strlen(str); i++)
    			*(str+i) = toupper(*(str+i));
    	}
    :yanks out hair:

  5. #5
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    Quote Originally Posted by anonytmouse
    *(str+i) is the very ugly equivalent of str[i].
    the book says to use pointer notation wherever possible. but yes. it is ugly.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
       void upit()    //convert to uppercase if necessary
       {
          for ( char *s = str; *s; ++s )
          {
             *s = toupper(*s);
          }
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Ok, so I believe that a string literal is a constant in C++, and therefore you are not allowed try to change a string literal. As a result, you can't do this:
    Code:
    char* p = "some text";
    cout<<p[0]<<endl; //'s'
    p[1]='t';  //no compiler error but crashes program at runtime 
    cout<<p<<endl;
    However, you can do this:
    Code:
    char c[20] = "other text";
    c[0] = 't';
    cout<<c<<endl;
    which looks like the same thing. So, when you assign a string to a char array, is there some kind of internal strcpy() going on that copies the constant string into the char array c?
    Last edited by 7stud; 02-09-2005 at 10:47 PM.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> So, when you assign a string to a char array, is there some kind of internal strcpy() going on that copies the constant string into the char array c? <<

    Yes, your deduction is exactly right. You can also use:
    Code:
    char c[] = "other text";
    This will automatically create an array big enough to hold the string literal.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>which looks like the same thing.
    Indeed it does, but this is in fact one of the rare places that the distinction between an array and a pointer becomes apparent.

    So, when you assign a string to a char array, is there some kind of internal strcpy() going on that copies the constant string into the char array c?
    It behaves this way at initialization, but attempting to do so at any point in the future will generate a compiler error saying "cannot convert from 'const char [5]' to 'char [5]'".
    Last edited by Hunter2; 02-09-2005 at 10:48 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. toupper() function
    By strokebow in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2008, 10:54 AM
  2. toupper(); tolower(); ?
    By dune911 in forum C Programming
    Replies: 9
    Last Post: 01-07-2003, 06:40 PM
  3. need example of toupper()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2002, 10:07 AM
  4. conio.h, toupper(), and a craps game
    By loki221 in forum C Programming
    Replies: 2
    Last Post: 11-28-2001, 07:40 PM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM