Tuesday, 15 January 2013

c++ - Can't figure out why the number of spaces are not counted -



c++ - Can't figure out why the number of spaces are not counted -

i have been teaching learning c++ , i've been trying create programme takes string of characters , removes white spaces using pointers. working want ouput number of spaces removed. code looks right on tired eyes. have number of spaces defined spaces. pretty self explanatory i'm trying do. help appreciated! :)

#include <iostream> using namespace std; int stripwhite(char *str); int main() { char str[100]; cin.getline(str, 99); // save room null character. stripwhite(str); cout << str << endl; cout << "i removed " << stripwhite(str) << " sentence."; homecoming 0; } int stripwhite(char *str) { char *p; int spaces = 0; (p = str; *str != '\0'; ++str) { if (*str != 0x20) { *p++ = *str; } else { spaces++; } } *p = '\0'; homecoming spaces; }

because you're calling stripwhite on string twice (the first time throwing away number removed) there'll nil remove sec time.

you need phone call 1 time , save homecoming value, like:

int count = stripwhite(str); cout << str << endl; cout << "i removed " << count << " sentence.";

c++ arrays

No comments:

Post a Comment