Dropping the first character of a string

This is a discussion on Dropping the first character of a string within the C++ Programming forums, part of the General Programming Boards category; How would you drop or ignore the first character of a string? For example, you have a string that says ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    18

    Question Dropping the first character of a string

    How would you drop or ignore the first character of a string? For example, you have a string that says "Spaghetti" and you just want "paghetti".

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    int main()
    {
    	std::string buffer("Spaghetti");
    	buffer.erase(0,1);
    	std::cout << buffer << std::endl;
    
    	return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    e:\dev\textfile\textfile.cpp(36) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable convers
    ion)

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    string orig = "Spaghetti";
    string result = orig.substr(1);
    cout<<result<<endl;
    That version of substr() gets the portion of the string starting at index position 1 to the end of the string. Strings like arrays start with index position 0.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by suzumebachi
    e:\dev\textfile\textfile.cpp(36) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable convers
    ion)

    The following should be assumed for the code sample provided to work properly...

    Code:
    #include <string>
    #include <iostream>
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    ahh.. i was using string.h

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    huzzah it works! thanks guys...

    Code:
    int main()
    {
    	int length;
    	char capital;
    	char text[]="this is some text";
    	char space[]=" ";
    	char *result=NULL;
    
    	result = strtok(text,space);
    	while (result != NULL)
    	{
    		length = strlen(result);
    		strncpy(&capital, result, 1);
    		std::cout << (char)toupper( (int)capital );
    		std::string buffer(result);
    		buffer.erase(0,1);
    		std::cout << buffer << " ";
    		result = strtok( NULL, space );
    	}
    	return 0;
    }

    edit: ok the above works fine, but if i try to turn it into a seperate function it compiles just fine but crashes when you try to run it:

    Code:
    void propercase(char input[])
    {
    	int length;
    	char capital;
    	char space[]=" ";
    	char *result=NULL;
    
    	result = strtok(input,space);
    	while (result != NULL)
    	{
    		length = strlen(result);
    		strncpy(&capital, result, 1);
    		std::cout << (char)toupper( (int)capital );
    		std::string buffer(result);
    		buffer.erase(0,1);
    		std::cout << buffer << " ";
    		result = strtok( NULL, space );
    	}
    }
    
    int main()
    {
    	propercase("this is some text");
    	return 0;
    }
    Last edited by suzumebachi; 05-02-2005 at 01:32 PM.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    strncpy(&capital, result, 1);
    Why not just:
    Code:
    capital = result[0];
    Code:
    std::string buffer(result);
    buffer.erase(0,1);
    std::cout << buffer << " ";
    If all you are trying to do here is display the "remainder" of the word (less the first character you are converting to uppercase) you could have either just converted the remainder to a string directly (skipping the erase bit):

    Code:
    std::string buffer(result+1);
    Or you don't really need to even bother going through the motions of converting it to a string at all just so you can easily remove the first character and have something to display. You can just output the remainder directly using cout without converting it to a string:

    Code:
    std::cout << result+1 << " ";
    A more C++/OO version of this program would likely use a stringstream and the standard stream extraction operators in place of the strtok function.

    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    #include <cctype>
    
    int main()
    {
        std::stringstream sstr("this is some text");
        std::string word;
    
        while( sstr >> word )
            std::cout << (char)toupper(word[0]) << word.substr(1) << " ";
    
        return 0;
    }
    On to your problem:

    Quote Originally Posted by suzumebachi
    edit: ok the above works fine, but if i try to turn it into a seperate function it compiles just fine but crashes when you try to run it:
    I suspect this is because you are passing a string literal into the function directly which is placed in read only memory. The strtok function needs to modify this data and can't if it is read only. You need to create an actual char array and then pass that in to the function. I.e. change this...
    Code:
    int main()
    {
        propercase("this is some text");  // This is a string literal, i.e. read-only memory
        return 0;
    }
    Into this...
    Code:
    int main()
    {
        char text[] = "this is some text";
        propercase(text);
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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