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".
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".
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
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)
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.Code:string orig = "Spaghetti"; string result = orig.substr(1); cout<<result<<endl;
Originally Posted by suzumebachi
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
ahh.. i was using string.h
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.
Why not just:Code:strncpy(&capital, result, 1);
Code:capital = result[0];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); buffer.erase(0,1); std::cout << buffer << " ";
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::string buffer(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:std::cout << result+1 << " ";
On to your problem: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; }
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...Originally Posted by suzumebachi
Into this...Code:int main() { propercase("this is some text"); // This is a string literal, i.e. read-only memory return 0; }
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