How to extract words from char type string

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: How to extract words from char type string

 
0
  #11
Jun 15th, 2007
The first loop of for -understood
but from ---

i=o

i cant understand

if(a[x]==' ')
.
.
.
end

please give a few statements regarding these lines I am unable to get it. the 2-d array is used to tranfser words separately into it

but after that unable to get it specially b[i][j]==' '
because this array is containg words only right now in for loop
Last edited by hinduengg; Jun 15th, 2007 at 10:44 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 5
Reputation: pengwn is an unknown quantity at this point 
Solved Threads: 1
pengwn pengwn is offline Offline
Newbie Poster

Re: How to extract words from char type string

 
1
  #12
Jun 15th, 2007
this part is because after the "up" there is no space
check for "\0"
we store a new word "up" in b[2][0]... or second row of
b[10x20] array.
here wrd=2 and i=0,1,2.. that is 0=u, 1=p, 2='\0'

  1. i=0;
  2. if(a[x] == '\0')
  3. for(p=m;p <= x;p++)
  4. b[wrd][i++] = a[p];
  5. b[wrd][i] = '\0';

here b[i][j] is just that i = wrd and j is the stings
jack
went
up
where b[2][0] = u
b[1][0] = w
b[0][0] = j

  1. for(i=wrd; i >= 0 ; i--) {
  2. int j = 0;
  3. while( b[i][j] != '\0')
  4. cout << b[i][j++];
  5. cout << endl;
  6. }

hope you get the idea. just throw in some cout statments in the
code and print b[i][j] you will get it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: How to extract words from char type string

 
0
  #13
Jun 15th, 2007
Thank you very much. The solution was great.I understood!!!!!
Last edited by hinduengg; Jun 15th, 2007 at 11:39 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to extract words from char type string

 
0
  #14
Jun 15th, 2007
Originally Posted by pengwn View Post
the below will reverse your sentence it works.

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5. char a[100];
  6. char b[10][20];
  7. int x,m = 0,p,wrd = 0, i = 0;
  8.  
  9. cout<<"Enter a sentence"<<endl;
  10. gets(a);
  11.  
  12. for(x=0; a[x] != '\0'; x++) {
  13. if(a[x] == ' ') {
  14. for(p=m;p <= x;p++)
  15. b[wrd][i++] = a[p];
  16. b[wrd][i] = '\0';
  17. m = x + 1;
  18. wrd++;
  19. i = 0;
  20. }
  21. }
  22. i=0;
  23. if(a[x] == '\0')
  24. for(p=m;p <= x;p++)
  25. b[wrd][i++] = a[p];
  26. b[wrd][i] = '\0';
  27.  
  28. for(i=wrd; i >= 0 ; i--) {
  29. int j = 0;
  30. while( b[i][j] != '\0')
  31. cout << b[i][j++];
  32. cout << endl;
  33. }
  34. }
IMO it would have been better to have left her to her own devices to come up with a solution, instead of writing the entire code. All she needs was a few helping hints.

Also, why use an example with gets(). Show her what she should use...
http://www.daniweb.com/tutorials/tutorial45806.html or even cin.getline?
Last edited by iamthwee; Jun 15th, 2007 at 2:20 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to extract words from char type string

 
0
  #15
Jun 15th, 2007
>IMO it would have been better to have left her to her own devices to
>come up with a solution, instead of writing the entire code.
True, but in this case...not so much of an issue. That solution is rather poorly implemented and overly complex. If you're going to use a multidimensional array, the problem is trivial and takes practically no thought at all:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. char words[10][100];
  8. int i = 0;
  9.  
  10. cout<<"Enter a sentence: ";
  11.  
  12. while ( i < 10 && cin>> words[i] )
  13. ++i;
  14.  
  15. while ( --i >= 0 )
  16. cout<< words[i] <<' ';
  17. cout<<'\n';
  18. }
The real problem is when you have to do the reversal in-place. Even then, I could still do it with less complexity than pengwn managed to put into the simpler solution.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: How to extract words from char type string

 
0
  #16
Jun 15th, 2007
[quote=Narue;388985]
True, but in this case...not so much of an issue. That solution is rather poorly implemented and overly complex. If you're going to use a multidimensional array, the problem is trivial and takes practically no thought at all:
[code]
#include <iostream>

using namespace std;

int main()
{
char words[10][100];
int i = 0;

cout<<"Enter a sentence: ";

while ( i < 10 && cin>> words[i] )
++i;

while ( --i >= 0 )
cout<< words[i] <<' ';
cout<<'\n';
}


Thanks a lot for such a precise solution .
But i am a little confused about 2 things

-can we really treat char arrays as integers and use cin>>words[i] (char type)

int a;
like cin>>a

-Secondly if words[10][100] is a 2-d array then can we really use as a single dimensional array like you have used in

cout<<words[i]

Please help !!!
Last edited by hinduengg; Jun 15th, 2007 at 11:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: How to extract words from char type string

 
0
  #17
Jun 16th, 2007
I TRIED YOUR SOLUTION AND IT IS WORKING WELL .

I again got it but this time very easily .Your solution was very accurate,
but in this case we will have to give the no. of strings entered initially like you have given it 10

same way should it be
int n;
cin>>n;
words[n][100];

By the way could you tell me how do we daclare char arrays of "n-size".Its dynamic allocation right?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to extract words from char type string

 
0
  #18
Jun 16th, 2007
>-can we really treat char arrays as integers and use cin>>words[i] (char type)
You can use cin's >> operator with any data type that overloads that operator. That means all of the built-in types and any custom types that support the operator. So yes, you can use cin's >> operator to read a C-style string. However, while I kept my example simple, to be strictly correct with an array of char you should take care to restrict the field width so that the array isn't overfilled:
  1. #include <iomanip>
  2.  
  3. ...
  4.  
  5. while ( i < 10 && cin>> setw ( 100 ) >> words[i] )
I say that because I can see you using this instead of getline to read strings, and/or not using the C++ string type for a while.

>-Secondly if words[10][100] is a 2-d array then can we really use as a single dimensional array
A two dimensional array is really just an array of arrays. If you index the first dimension, you get a one dimensional array and can use it as a one dimensional array.

>same way should it be
>int n;
>cin>>n;
>words[n][100];
No, C++ doesn't support non-constant array sizes, so you can't declare an array with user input. However, you can simulate an array using pointers. Most of the time you'll want to do this with both dimensions, but it's actually easier (though a tad more obscure) to do it only with the first dimension:
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. const int size = 100;
  9. int n;
  10.  
  11. cout<<"Number of words: ";
  12.  
  13. if ( cin>> n ) {
  14. char (*words)[size] = new char[n][size];
  15.  
  16. cout<<"Enter a sentence: ";
  17.  
  18. int i = 0;
  19.  
  20. while ( i < n && cin>> setw ( size ) >> words[i] )
  21. ++i;
  22.  
  23. while ( --i >= 0 )
  24. cout<< words[i] <<' ';
  25. cout<<'\n';
  26.  
  27. // Always release what you allocate
  28. delete[] words;
  29. }
  30. }
>By the way could you tell me how do we daclare char arrays of
>"n-size".Its dynamic allocation right?
Yes. It goes like this:
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. // Use the new[n] operator to
  9. // allocate memory to a pointer
  10. char *p = new char[100];
  11.  
  12. // Use it pretty much like an array
  13. if ( cin.getline ( p, 100 ) )
  14. cout<<">"<< p <<"<\n";
  15.  
  16. // Use the delete[] operator to
  17. // release memory allocated with new[n]
  18. delete[] p;
  19. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: hinduengg is an unknown quantity at this point 
Solved Threads: 3
hinduengg's Avatar
hinduengg hinduengg is offline Offline
Junior Poster in Training

Re: How to extract words from char type string

 
0
  #19
Jun 16th, 2007
Thank you a lot for the timely help.You have cleared the biggest doubt regarding C++ strings.
But here I request you to tell me

if ( cin>> n ) {
char (*words)[size] = new char[n][size];


My professor has not taught about pointers for that is not in course but I
think to learn it .
Please could you explain this line to me as it as poniters.
I know one thing that pointers indicate to a value but after that no clue.
Please do not be irritated.I am trying to understand.I assume that is just the syntax.
right? But this for 2-d array of char type but on Left you have used 1-d char array

I think you are again using 2 D array as 1D array

Similarly it will be
int n;
int *a[100]=new[n];(for integer types)

char*wd[size]=new[n];(for 1-d char array)




while(i<n && cin>>setw(100)>>words[i];


Besides what is meant by overfilling of the array that you use setw() here.
I have beentold that it is used to specify minimum no. of charecter positions on the output field
a varaible will consume.

PLEASE PERTAIN TO ONLY C++ FOR THATS IN MY SYLLABUS
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to extract words from char type string

 
0
  #20
Jun 16th, 2007
>I know one thing that pointers indicate to a value but after that no clue.
This should clear up several things about pointers.

>but on Left you have used 1-d char array
But it's not. It's a pointer to a one dimensional array. In this case you can think of it as a two dimensional array where the first dimension is set dynamically.

>I think you are again using 2 D array as 1D array
No, I'm using a one dimensional array as a one dimensional array. A two dimensional array is an array of arrays. If you declare ma as a two dimensional array, ma[0] is a one dimensional array.

>Besides what is meant by overfilling of the array that you use setw() here.
You need to tell cin when to stop. If you don't, it'll stop when it reaches whitespace. But what happens if the input stream contains 50000 characters with no whitespace? Well, cin will try to write all 50000 characters to your array. If your array can only hold 100 characters, that's a really bad thing.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC