| | |
Using pointers to strip sentence of spaces
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
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!
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)
#include <iostream> using namespace std; void print(char sentence2[],int size);//function to print sentence when done void stripWhite(char point[],char sentence2[],int size);//uses pointers to strip sentence of spaces int main () {//main char sentence1[1000];//first array to obtain sentence int size;//integer to decide how big new array should be cout << "Please enter a sentence and I will strip it of all spaces." << endl; cin >> sentence1;//input for first array for(int i=0;i<1000;i++)//for loop to find out size of array {//for if(sentence1[i] >= 'a' && sentence1[i] <= 'z' || sentence1[i] == ' ' || sentence1[i] == ',' || sentence1[i] == '.') {//if size++; }//if }//for char sentence2[size];//new array with exact size it should be for(int y;y<size;y++)//for loop to copy contents of first array to second {//for sentence2[y]=sentence1[y]; }//for char point;//soon to be pointer for(int y;y<size;y++)//for loop to get point to become a pointer to second array {//for point[y]=&sentence2[y]; }//for cout << endl; stripWhite(sentence2,point,size); return 0; }//main void print(char sentence2[],int size)//print function {//void print for(int z;z<size;z++) {//for cout << sentence2[z]; }//for }//void char stripWhite(char point[],char sentence2[],int size)//function to strip sentence of spaces {//char stripWhite for(int y;y<size;y++)//for loop to only set non space characters to second array {//for if(point[y]!=' ')//if to decide if it is a space or not {//if sentence2[y]=*point[y]; }//if }//for print(sentence2,size);//function call to print finished sentence }//char
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);
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)
//str is the string passed. Key is the key that seperates the word //does not use pointers. void Strip(std::string str, char key) { string Temp = ""; for(int i = 0; i < str.size(); i++) //Run through the string { if(str[i] == key) { // copy str from 0 to i into temp } } return Temp; }
1) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
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);
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.
•
•
Join Date: Apr 2008
Posts: 64
Reputation:
Solved Threads: 1
0
#4 Oct 6th, 2009
•
•
•
•
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.
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!!
![]() |
Similar Threads
- Code correction - counting characters. (Python)
- removing spaces from string (C++)
- Handling text files and applying defined template (Python)
- URGENT help needed with strings. (C++)
- help me (C)
Other Threads in the C++ Forum
- Previous Thread: Converting binary to decimal : beginner
- Next Thread: Hexadecimal Blues!!!!
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






