How to extract words from char type string

Thread Solved
Reply

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

How to extract words from char type string

 
0
  #1
Jun 14th, 2007
HELLO THIS IS MY FIRST POST.

I WISHED IF ANY ONE COULD HELP IN EXTRACTING WORDS FROM A CHAR ARRAY -STRING

LIKE IF THE SENTENCE INPUTTED BY USER - JACK WENT UP
THEN I NEED TO PRINT THESE WORDS SEPARATELY ON FRESH LINES
OUTPUT-
JACK
WENT
UP


I TRIED THIS BUT THIS IS NOT WORKING

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. void main()
  5. char a[100];
  6. int x;
  7. cout<<" enter a sentence";
  8. gets(a);
  9. for(int x=0;a[x]!='\0';x++)
  10. {
  11. if(a[x]==' ')
  12. {
  13. for(int p=0;p<=x;p++)
  14. cout<<a[p];
  15.  
  16. cout<<endl; }
  17. }
  18. }
the output produced is

JACK
JACK WENT
JACK WENT UP

JUST ONE MORE REQUEST
THE PROGRAM SHOULD BE DONE ONLY BY SIMPLE CHAR TYPE ARRAYS PLEASE.

I HAVE TRIED THIS QUERY ON OTHER FORUMS BUT ALL ADVICED TO USE "STDL "STRINGS WHICH IS NOT IN MY COURSE.

YOU CAN USE MULTIDIMENSIONAL ARRAYS FOR STORING WORDS

HOPING FOR A SOLUTION
Last edited by ~s.o.s~; Jun 14th, 2007 at 1:10 pm. Reason: Added code tags.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: How to extract words from char type string

 
0
  #2
Jun 14th, 2007
Your output is wrong because you're starting the inner for loop at 0 every time.So when a[x] == ' ' it's writing start point to ' '.

You should save the ' ' character's index in a variable and start the loop at this variable.

Finally there is not a ' ' character at the sentence's and so you should repeat the inner for once again outside the first loop(i.e,before return 0).

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main(){
  5.  
  6. char a[100];
  7. int x,m = 0,p;
  8.  
  9. cout<<"Enter a sentence"<<endl;
  10. gets(a);
  11.  
  12. for(x=0;a[x]!='\0';x++)
  13. {
  14. if(a[x]== ' ')
  15. {
  16. for(p=m;p <= x;p++)
  17. cout<<a[p];
  18. m = x + 1;//For not writing the space character at the //word's lead.You may do m = x;
  19. }
  20.  
  21. cout<<endl;
  22. }
  23.  
  24. for(p = m; p <= x;p++)
  25. cout<<a[p];
  26.  
  27. return 0;
  28.  
  29. }

And note that "gets()" function is dangereous .Try to fgets() instead.
Last edited by FoX_; Jun 14th, 2007 at 12:22 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
  #3
Jun 14th, 2007
Thank you so much for the wonderful solution .
I had been trying so hard to get the correct solution ,THANKS A LOT.

But I have 1 query-
Why gets() is dangerous ,what and how do we use fgets ,I mean is there a special prototype?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: How to extract words from char type string

 
0
  #4
Jun 14th, 2007
Your welcome and look at this link:
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: How to extract words from char type string

 
0
  #5
Jun 14th, 2007
I mean You're welcome...
Last edited by FoX_; Jun 14th, 2007 at 8:39 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
  #6
Jun 15th, 2007
I am not good at strings . So please could you help me in getting this question right?

The question is -if the user inputted -' 'JACK WENT UP' '
then the output should be -

UP WENT JACK

I tried to reverse the string from the last space position but that is not working but cant get the second last index having a space .

Please please help!!
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
  #7
Jun 15th, 2007
Please ,the solution must only include functions related to char type strings or otherwise I will not be able to comprehend .

Well my sir told to transfer the words separately in to the rows of a 2 dimensional array and then print them accordingly,but I am unable to get the hang of it.

Please help.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How to extract words from char type string

 
0
  #8
Jun 15th, 2007
Post your latest code so that it would be easier for someone to help you out.
I don't accept change; I don't deserve to live.
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
  #9
Jun 15th, 2007
iI cant get the hang of it

the code is the same as above

#include<iostream>
using namespace std;

int main(){

char a[100];
int x,m = 0,p;

cout<<"Enter a sentence"<<endl;
gets(a);

for(x=0;a[x]!='\0';x++)
{
if(a[x]== ' ')
{
for(p=m;p <= x;p++)
cout<<a[p];
m = x + 1;//For not writing the space character at the //word's end.
}

cout<<endl;
}

for(p = m; p <= x;p++)
cout<<a[p];
cout<<m<<"last space charecter";

for(int r=m;r>=m;r--)
cout<<a[p];


}

The word order needs to be reversed .(I know want to know how do we get the space positions from end to beginning.)


FOR EXAMPLE-

if you had -' 'JACK WENT UP' '
0-' '
1-J
2-A
3-C
4-K
5-' '
6-W
7-E
8-N
9-T
10-' '
11-U
12-P
13-' '

I want to print words this way- from 10 position to 13 position
from 5 position to 10 position
from 0 position to 5 position


I have tried a lot but not got it correct
Please help me in this !!!
Last edited by hinduengg; Jun 15th, 2007 at 4:28 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

 
0
  #10
Jun 15th, 2007
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. }
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