Saturday, May 22, 2010

How to detect if a string has a space in it in C++?

If it is a C++ string you can use the find or the rfind functions. The find function returns the position of a substring from the left to right and the rfind from the right to left. Both return string::npos if the substring is not found. You can then test





if(s.find(" ") != string::npos)


// there is a space on the string


// do something


{





}

How to detect if a string has a space in it in C++?
You've gotta scan through each character one by one until you reach the end or find a space.





You can use wcschr to do that:





// use const char * for ANSI strings


bool contains_a_space(const wchar_t *s)


{


// use strchr(s, ' ') for ANSI strings


return wcschr(s, L' ') != NULL;


}





For std::wstring then you can do...





// use std::string for ANSI strings


bool contains_a_space(const std::wstring %26amp;s)


{


// use s.find(' ') for ANSI strings


return s.find(L' ') != -1;


}
Reply:hmmm, my mind is cloudy right now, this isspace() function works but it's for char in the cctype library, you can always make a strange a char by declaring it like this:





char anything[64];





the 64 is the amount of space expected, you can make it larger, can't remember if you can leave it blank and it self detects.





here is something i wrote a long time ago, it counts characters, spaces, i dunoo what else in a text file, perhaps this will help you:





#include %26lt;iostream%26gt;





#include %26lt;fstream%26gt;





#include %26lt;cstdlib%26gt;











int main()





{





using namespace std;











ifstream fin;





ofstream fout;





char in_file[32], out_file[32], character;





int alpha_count(0), white_count(0), nonwhite_count(0), total(0);











cout %26lt;%26lt; "Please enter filename to read from: ";





cin %26gt;%26gt; in_file;





cout %26lt;%26lt; "Please enter filename to write to: ";





cin %26gt;%26gt; out_file;











fin.open(in_file);





if (fin.fail())





{





cout %26lt;%26lt; "Opening input file failed. \n";





system("pause");





exit(1);





}











fout.open(out_file);











while (! fin.eof())





{





fin.get(character);











if (isalpha(character))





alpha_count++;





else if (isspace(character))





white_count++;





else





nonwhite_count++;





}











total = alpha_count + white_count + nonwhite_count;











cout %26lt;%26lt; "The total count of characters is " %26lt;%26lt; total %26lt;%26lt; endl





%26lt;%26lt; "Total count of non-whitespace characters is " %26lt;%26lt; nonwhite_count + alpha_count %26lt;%26lt; endl





%26lt;%26lt; "Total count of lettere is " %26lt;%26lt; alpha_count %26lt;%26lt; endl %26lt;%26lt; endl;











fout %26lt;%26lt; "The total count of characters is " %26lt;%26lt; total %26lt;%26lt; endl





%26lt;%26lt; "Total count of non-whitespace characters is " %26lt;%26lt; nonwhite_count + alpha_count %26lt;%26lt; endl





%26lt;%26lt; "Total count of lettere is " %26lt;%26lt; alpha_count %26lt;%26lt; endl %26lt;%26lt; endl;











fin.close();





fout.close();











system("pause");





return 0;





}





------notes------





this is the complete program, you can cut and paste it to run it, when you indicate the file make sure to put .txt





hope that helps.





hope there isn't many errors, i wrote that for a class i was taking a long time ago.


No comments:

Post a Comment