943,946 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 580
  • C++ RSS
Oct 5th, 2009
0

Using pointers to strip sentence of spaces

Expand Post »
Hello, this seems like it will be a helpful community. For my degree I need to take a basic C++ class and the only C++ class the offer that fits is an 8 week class, it really goes too fast. Anyways, I've been trying my hardest, but my brain really doesn't think in code, so I've probably made quite a few dumb errors that I can't see.

For this assignment the teacher wants us to write a program that will take an input of a sentence. Then he wants us to pass it to a function that uses pointers to strip the sentence of all spaces. It's really throwing me for a loop, I think I might be on the right track, although it's giving me several compile errors right now. Any help would be greatly appreciated!

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void print(char sentence2[],int size);//function to print sentence when done
  5.  
  6. void stripWhite(char point[],char sentence2[],int size);//uses pointers to strip sentence of spaces
  7.  
  8. int main ()
  9. {//main
  10.  
  11. char sentence1[1000];//first array to obtain sentence
  12.  
  13. int size;//integer to decide how big new array should be
  14.  
  15. cout << "Please enter a sentence and I will strip it of all spaces." << endl;
  16.  
  17. cin >> sentence1;//input for first array
  18.  
  19. for(int i=0;i<1000;i++)//for loop to find out size of array
  20. {//for
  21. if(sentence1[i] >= 'a' && sentence1[i] <= 'z' || sentence1[i] == ' ' || sentence1[i] == ',' || sentence1[i] == '.')
  22. {//if
  23. size++;
  24. }//if
  25. }//for
  26.  
  27. char sentence2[size];//new array with exact size it should be
  28.  
  29. for(int y;y<size;y++)//for loop to copy contents of first array to second
  30. {//for
  31. sentence2[y]=sentence1[y];
  32. }//for
  33.  
  34. char point;//soon to be pointer
  35.  
  36. for(int y;y<size;y++)//for loop to get point to become a pointer to second array
  37. {//for
  38. point[y]=&sentence2[y];
  39. }//for
  40.  
  41. cout << endl;
  42.  
  43. stripWhite(sentence2,point,size);
  44.  
  45. return 0;
  46. }//main
  47.  
  48. void print(char sentence2[],int size)//print function
  49. {//void print
  50.  
  51. for(int z;z<size;z++)
  52. {//for
  53. cout << sentence2[z];
  54. }//for
  55.  
  56. }//void
  57.  
  58. char stripWhite(char point[],char sentence2[],int size)//function to strip sentence of spaces
  59. {//char stripWhite
  60.  
  61. for(int y;y<size;y++)//for loop to only set non space characters to second array
  62. {//for
  63. if(point[y]!=' ')//if to decide if it is a space or not
  64. {//if
  65. sentence2[y]=*point[y];
  66. }//if
  67. }//for
  68.  
  69. print(sentence2,size);//function call to print finished sentence
  70.  
  71. }//char
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
that_fox is offline Offline
2 posts
since Oct 2009
Oct 5th, 2009
0

Re: Using pointers to strip sentence of spaces

If this is what you mean.

BTW, where is the pointers.

And a rule of thumb, when you are using char *, instead of string,
you have a bug(In my opinion);

C++ Syntax (Toggle Plain Text)
  1. //str is the string passed. Key is the key that seperates the word
  2. //does not use pointers.
  3. void Strip(std::string str, char key)
  4. {
  5. string Temp = "";
  6.  
  7. for(int i = 0; i < str.size(); i++) //Run through the string
  8. {
  9. if(str[i] == key)
  10. {
  11. // copy str from 0 to i into temp
  12. }
  13. }
  14. return Temp;
  15. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Oct 5th, 2009
0

Re: Using pointers to strip sentence of spaces

If this is what you mean.

BTW, where is the pointers.

And a rule of thumb, when you are using char *, instead of string,
you have a bug(In my opinion);
I thought that when I put "point[y]=&sentence2[y];" it was getting point to refer to the memory space instead of the value. And then in the stripWhite function "sentence2[y]=*point[y];" was referring to the value within the pointer. Is that not using pointers? I'm sorry to say I'm confused when it comes to pointers. How would I get my program to use pointers if it is not already?

And also, I am not against using strings instead of character arrays, it's just that my professor did not touch on strings much. Although I could use them in this program.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
that_fox is offline Offline
2 posts
since Oct 2009
Oct 6th, 2009
0
Re: Using pointers to strip sentence of spaces
Click to Expand / Collapse  Quote originally posted by that_fox ...
I thought that when I put "point[y]=&sentence2[y];" it was getting point to refer to the memory space instead of the value. And then in the stripWhite function "sentence2[y]=*point[y];" was referring to the value within the pointer. Is that not using pointers? I'm sorry to say I'm confused when it comes to pointers. How would I get my program to use pointers if it is not already?

And also, I am not against using strings instead of character arrays, it's just that my professor did not touch on strings much. Although I could use them in this program.
You are close on the "sentence2[y]=*point[y];" part but sentence2[y] must be a pointer. As you have it in your code it is just a plain old variable. The & does return the address of the object, in this case however you cant assign that address to a character. While the compiler might let you do it(if you assign an int to a char it will set the char to be whatever value the char is on the ascii table) I just wrote this little tutorial on pointers. See if it helps. Check the end to see about references.

http://mikectamu.blogspot.com/2009/1...explained.html

As far as the diff between strings and pointer arrays, in C there really is no difference, but in C++ string objects are handled a little differently. A character array can easily be used as a string. I would try to debug your program but since you aren't even using pointers and will have to rewrite it anyways it would seem to be a waste of time. Try rewriting it with the pointers in tact and then if you have problems post it up on here.

Hope that helps!!
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Converting binary to decimal : beginner
Next Thread in C++ Forum Timeline: Hexadecimal Blues!!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC