String Class

This is a discussion on String Class within the C++ Programming forums, part of the General Programming Boards category; toupper, which I think is in <cctype>. It converts one character at a time....

  1. #106
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    toupper, which I think is in <cctype>. It converts one character at a time.

  2. #107
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by BKurosawa View Post
    Do you know how to convert to upper case letters? Is there a predefined function that does that?
    you can convert it one character at a time with the toupper() function from <cctype>. The std::transform function from the <algorithm> library is handy here -
    Code:
        std::transform(buf, buf+stringlength, buf, toupper );

  3. #108
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think the unary + shouldn't do anything. At least, that's what it does for numeric types.

    Unary - might return a reversed string?
    Last edited by anon; 08-07-2007 at 04:13 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #109
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Thanks everybody for your help so far. I am pretty close to finishing, but the problem right now is that my reverse string function (shown below) isn't reversing the string. It is only outputting the string multiple times. I do not know why. I have supplied the reverse string function and the test from the driver that utilizes it.

    Code:
    
    ReverseString ReverseString::operator ~()
    {
    	char *temp = buf;
    	for(int i = stringlength; i>= 0; i--)
    	{
    		swap(temp[stringlength - i], temp[i]);
    	}
    	return temp;
    }
    
    
    void test19() {
    
        system("cls");
    
        cout << "19. Testing: ReverseString class." << endl << endl;
    
        csis << "19. Testing: ReverseString class." << endl << endl;
    
        ReverseString s19("Purusha");
    
        ReverseString t19;
    
        s19.setName("s19");
    
        t19.setName("t19");
    
        t19 = ~s19;
    
        s19.print();
    
        t19.print();
    
     
    
        ReverseString u19(~~s19);
    
        u19.setName("u19");
    
        u19.print();
    
        wait();
    
    }

  5. #110
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    [QUOTE=BKurosawa;662796]
    Code:
    ReverseString ReverseString::operator ~()
    {
    	char *temp = buf;
    	for(int i = stringlength; i>= 0; i--)
    	{
    		swap(temp[stringlength - i], temp[i]);
    	}
    	return temp;
    }
    You should only swap half the string (because once you are past the middle, you are swapping it back again - think about it...)

    Also, if your swap works as a proper swap, you don't need to swap it "from the back", do you?

    --
    Mats

  6. #111
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    [QUOTE=matsp;662798]
    Quote Originally Posted by BKurosawa View Post
    Code:
    ReverseString ReverseString::operator ~()
    {
    	char *temp = buf;
    	for(int i = stringlength; i>= 0; i--)
    	{
    		swap(temp[stringlength - i], temp[i]);
    	}
    	return temp;
    }
    You should only swap half the string (because once you are past the middle, you are swapping it back again - think about it...)

    Also, if your swap works as a proper swap, you don't need to swap it "from the back", do you?

    --
    Mats
    I changed the code and it is still doing the same thing

    Code:
    ReverseString ReverseString::operator ~()
    {
    	int k = 0;
    	char *temp = buf;
    	for(int i = stringlength; i>=(i/2); i--)
    	{
    		swap(temp[k], temp[i]);
    		++k;
    	}
    	return temp;
    }

  7. #112
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    buf[stringlength] = '\0'
    you want to leave it as a last char - not to move it in he first cahr effectively truncating the string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #113
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    buf[stringlength] = '\0'
    you want to leave it as a last char - not to move it in he first cahr effectively truncating the string
    I still don't have it. What is wrong with this? Thanks for the help BTW.

    Code:
    ReverseString ReverseString::operator ~()
    {
    	int k = 0;
    	char *temp = new char[size];
    	temp = buf;
    	for(int i = stringlength-1; i>=(stringlength/2); i--)
    	{
    		k++;
    		swap(temp[k], temp[i]);
    		
    	}
    	temp[stringlength] = '\0';
    	return temp;
    }

  9. #114
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    Not having taken the time to reread the previous posts, I am wondering whether you defined the conversion of char* to ReverseString.

    And also... what does this do?
    Code:
    temp = buf;
    What happens to the memory you allocated?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #115
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by CodeMonkey View Post
    Not having taken the time to reread the previous posts, I am wondering whether you defined the conversion of char* to ReverseString.

    And also... what does this do?
    Code:
    temp = buf;
    What happens to the memory you allocated?
    You are right temp= buf; should not be there. No I did not define conversion of char* to ReverseString.

  11. #116
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    what is wrong with this code?

    Code:
    ReverseString ReverseString::operator ~()
    {
    	int k = 0;
    	char *temp = new char[size];
    	for(int i = stringlength-1; i>=(stringlength/2); i--)
    	{
    		k++;
    		swap(temp[k], temp[i]);
    		
    	}
    	temp[stringlength] = '\0';
    	return temp;
    }
    Thanks everybody!

  12. #117
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    920
    Did you forget something?

    Luckily, that isn't necessary. See if you understand this:
    Code:
        char* a = temp;
        char* b = orig + size;
        
        int count = size;
        while(count--) *a++ = *(--b);
        temp[size] = '\0';
    Last edited by CodeMonkey; 08-09-2007 at 02:04 AM. Reason: Vart was right
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  13. #118
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you increase k before swapping
    so temp[0] is leaved uninitialized
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 8 of 8 FirstFirst 12345678
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 11:43 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21