| | |
Using isspace with white spaces
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 67
Reputation:
Solved Threads: 0
im trying to take user input in the form of a sentence putting it in an array and then print out changes: Capitalize the first letter, then the rest of the letters are lower case. If there are multiple spaces, make them only one. Maybe I don't have to use isspace?
C++ Syntax (Toggle Plain Text)
const int SIZE = 100; int main() { char sentence[SIZE]; cout << "Please enter a short sentence: " << endl; cin.getline(sentence, SIZE); for (int i = 1; i < 100; i++) sentence[i] = tolower(sentence[i]); for (int i = 0; i < 1; i++) sentence[i] = toupper(sentence[i]); //if (isspace(sentence)) cout << sentence; return 0; system("PAUSE"); }
Last edited by matrimforever; Nov 17th, 2006 at 2:07 am.
Yeah. You dont. Consider the use of std::string and std:stringstream.
Anyway this should do for now.
Anyway this should do for now.
c Syntax (Toggle Plain Text)
const int SIZE = 100; int main() { /* * Remember to initialize char arrays */ char sentence[SIZE] = ""; std::cout << "Please enter a short sentence: " << std::endl; std::cin.getline(sentence, SIZE); /* * Remove leading spaces */ int i = 0; while( i < SIZE && isspace( sentence[i] )) { i++; }; /* * Check if the array has finished traversing */ if ( i == SIZE ) return 0; /* * Capitalize the first non-space character */ std::cout << (char)toupper(sentence[i++]); /* * Start after the leading spaces * Note that this will leave a trailing space, * but that wont be seen. */ for ( ; i < SIZE; ) { std::cout << (char)tolower(sentence[i]); if (isspace( sentence[i] )) { do { i++; } while( i < SIZE && isspace( sentence[i] ) ); } else { i++; } } return 0; }
Last edited by WolfPack; Nov 17th, 2006 at 3:28 am. Reason: Added bounds checking, changed do-while to for
バルサミコ酢やっぱいらへんで
•
•
•
•
Not sure how to output using a loop? Can you show me please?
C++ Syntax (Toggle Plain Text)
const int SIZE = 100; int main() { char sentence[SIZE]; cout << "Please enter a short sentence: " << endl; cin.getline(sentence, SIZE); for (int i = 1; i < 100; i++) { // sentence[i] = tolower(sentence[i]); cout << tolower(sentence[i]); // output } for (int i = 0; i < 1; i++) { // sentence[i] = toupper(sentence[i]); cout << toupper(sentence[i]); // output } // return 0; // wrong place // system("PAUSE"); // Yech!!!! Don't use this getchar(); // instead... return 0; // right place }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Here is one that removes the trailing spaces too.
c Syntax (Toggle Plain Text)
const int SIZE = 100; int main() { /* * Remember to initialize char arrays */ char sentence[SIZE] = ""; std::cout << "Please enter a short sentence: " << std::endl; std::cin.getline(sentence, SIZE); int len = strlen( sentence ); /* * Remove trailing spaces */ int i = len -1; while ( i >= 0 && isspace( sentence[i] ) ) { i--; } /* * Check if the array has finished traversing */ if ( i < 0 ) { return 0; } /* * Terminate the sentence before the trailing spaces and * update the new length */ sentence[ i + 1 ] = 0; len = strlen( sentence ); /* * Remove leading spaces */ i = 0; while( i < len && isspace( sentence[i] )) { i++; }; /* * Check if the array has finished traversing */ if ( i == len ) return 0; /* * Capitalize the first non-space character */ std::cout << (char)toupper(sentence[i++]); /* * Start after the leading spaces */ for ( ; i < len; ) { std::cout << (char)tolower(sentence[i]); if (isspace( sentence[i] )) { do { i++; } while( i < len && isspace( sentence[i] ) ); } else { i++; } } return 0; }
バルサミコ酢やっぱいらへんで
If you use C++ strings your life would be much simpler coz then you can do something like:
cpp Syntax (Toggle Plain Text)
#include <string> const std::string whiteSpaces( " \f\n\r\t\v" ); void trimRight( std::string& str, const std::string& trimChars = whiteSpaces ) { std::string::size_type pos = str.find_last_not_of( trimChars ); str.erase( pos + 1 ); } void trimLeft( std::string& str, const std::string& trimChars = whiteSpaces ) { std::string::size_type pos = str.find_first_not_of( trimChars ); str.erase( 0, pos ); } void trim( std::string& str, const std::string& trimChars = whiteSpaces ) { trimRight( str, trimChars ); trimLeft( str, trimChars ); }
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- trying to remove extra white spaces in data file (IT Professionals' Lounge)
- word count in borland c++ ?? (C++)
- file types: eliminating leading white spaces (C)
Other Threads in the C++ Forum
- Previous Thread: function call to determine a palindrome number
- Next Thread: Using a Database with C
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






