So i have this assignment to delete white space from a sentence using a pointers, well i was excited thinking the program was going to work first try, becuase i had no errors. WELL i was wrong........
heres the assignment:
heres what i had for codeUsing pointers write a function called stripWhite that will strip all of the white spaces from an array of characters. Your function should return the number of white spaces that you deleted.
White spaces can be a number of characters, but in our case we are only concerned with the blank space character (space bar) which is the character Decimal 32 or Hex 20.
Your prototype should be something like:
int stripWhite(char *str);
Your output should look something like the following:
Enter a sentence and I will strip out all spaces.
The quick brown fox jumped over the lazy old dog
Your sentence without spaces is:
Thequickbrownfoxjumpedoverthelazyolddog.
Note: Passing an array does not count as using pointers. So, if your only use of pointers is this:
int stripWhite(char *str)
{
}
Think again!
can anyone help me with what i have done wrong? i get this message (refer to pic below)Code:#include <iostream> #include <iomanip> #include <cstdlib> using namespace std; int stripWhite(char *str); int main() { char s; char *str = &s; char wo; cout << "Enter a sentence: "; cin >> *str; wo = stripWhite(str); cout << "Without Spaces:" << wo << endl; return 0; } int stripWhite(char *str) { char *p; p = str; do if (!isspace(*p=*str))p++; while (*str++); return *str; }
thank you everyone
rep for help!
The first problem you have in your program is using std::cin which only inputs a word not a sentence. So if you input "Hello my name is bob\0", you would only get "Hello\0". Try using std::cin.getline( str, 1024 ). The next problem is you are assigning the pointer str to point to s. This gives you one character of memory. If you enter anything even one letter, you will overflow the allocated memory because cin tacks on a "\0" (null character) at the end. Allocate an array like str[ 1024 ] and use it just like a pointer because arrays are secretly pointers. Another thing that would mess you up is the character wo. your function returns the number of characters deleted, so wo needs to be an int not a character or string. Remember to count the whitespace. I also left comments in some code i changed. I don't want to do your whole assignment for you, but you need to rethink the way your whitespace removing works. Remember str is a pointer to an array of characters so you can use pointer arithmetic to move through the array. once you've found the whitespace, try moving everything in the array back one.
Hope this helps a little. If you get stuck again, post it and I can get back to you. Its late and I'm going to sleep.
Code:#include <iostream> #include <iomanip> #include <cstdlib> using namespace std; int stripWhite(char *str); int main() { char str[ 1024 ]; // changed to an array int whitespace; // replaced wo with whitespace cout << "Enter a sentence: "; cin.getline( str, 1024 ); // cin.getline to get the whole line whitespace = stripWhite(str); // returns # of whitespaces // would print out # of whitespaces, not the // string itself, which is what you would want //cout << "Without Spaces:" << wo << endl; return 0; } int stripWhite(char *str) { char *p; p = str; int whitespace = 0; // whitespace counter, dont forget // try to rethink this area between here //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV do { // assigns the value of str to the value of p // doesn't do what you think here if (!isspace(*p=*str)) // remember to increase whitespace counter p++; // since p is a pointer, moves up one (one from start // of str) }while ( *str++ ); // very good use of pointer arithmetic!, but // you lose your spot //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // and here return whitespace; // return the number of whitespaces }
dang i wasnt even thinking about the cin part! that makes sense now.
ill tackle this later todya, its 5am and i have to go to work. UGH
well now its just a matter of getting the code to run according to the assignment. thats the hard part.
i feel lost on this project for some reason. lol
Well as atomicbaum said, your original code will cause a buffer overflow problem with entering even one character. So using getline with a array of characters would be your best bet. I don't see why you are using the do while loop when i basic for loop would be more relevant (at least to me).
Also in the instructions it says to print out the string with no spaces and the number of spaces found. In your code you could only do one of these.
You could do something like this that would find any space character in the string and get rid of it while counting the number of times it came across a space.
Or since the instructions said that you where only interested in the space bar character you could just doCode:#include <iostream> #include <iomanip> #include <cstdlib> using namespace std; void stripWhite(char *str); int main() { char str[256]; cout << "Enter a sentence: "; cin.getline(str,256); stripWhite(str); return 0; } void stripWhite(char *str) { char *p; p = str; int spaces = 0; int size = strlen(p); cout<<"your string without spaces is "; for(int i = 0; i < size; i++) { if(!isspace(p[i])) { cout<< p[i] ; //spaces++; } else { spaces++; } } cout<< endl <<"there where "<< spaces <<" spaces" <<endl; }
Which would only look for the space bar (0x20)Code:#include <iostream> #include <iomanip> #include <cstdlib> using namespace std; void stripWhite(char *str); int main() { char str[256]; //char *str = &s; cout << "Enter a sentence: "; cin.getline(str,256); stripWhite(str); return 0; } void stripWhite(char *str) { char *p; p = str; int spaces = 0; int size = strlen(p); cout<<"your string without spaces is "; for(int i = 0; i < size; i++) { //0x20 is the ascii code for the space bar if(p[i] != 0x20) { cout<< p[i] ; //spaces++; } else { spaces++; } } cout<< endl <<"there where "<< spaces <<" spaces" <<endl; }
i think my best route is to look for any spaces in the sentences only, after reading all your code, this project really makes sense now. its always easy to get it right after someone helps you though huh? haha well i appreciate all your help
Glad I could be of assistance, tho normally I would just point in the right direction instead of posting complete code but hey I was board lol.
Since others have already posted code to look at, here is more to compare/contrast with.
The key here is that writing to the destination is selective -- only if it is not whitespace does it copy the character pointed to by src into the location specified by dst, and the dst pointer is only incremented after it has done so.Code:#include <iostream> #include <cctype> using namespace std; int stripWhite(char *str); /** * This skips user input and simply provides a canned test driver. */ int main() { char text[] = "The quick brown fox jumps over the lazy dog"; cout << text << '\n'; stripWhite(text); cout << text << '\n'; return 0; } /** * Strip whitespace from a string. Note that the source and destination overlap. */ int stripWhite(char *src) { char *dst; // Use this pointer to write non-whitespace. for ( dst = src; // Point to the start of the source string. *src != '\0'; // Continue loop for all text in source string. ++src ) // Always increment source pointer. { if ( !isspace(*src) ) { *dst++ = *src; // Write and increment only if not whitespace. } } *dst = '\0'; // Ensure the modified string is null terminated. } /* my output The quick brown fox jumps over the lazy dog Thequickbrownfoxjumpsoverthelazydog */
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks