| | |
How to extract words from char type string
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
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
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
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<conio.h> #include<stdio.h> void main() char a[100]; int x; cout<<" enter a sentence"; gets(a); for(int x=0;a[x]!='\0';x++) { if(a[x]==' ') { for(int p=0;p<=x;p++) cout<<a[p]; cout<<endl; } } }
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.
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).
And note that "gets()" function is dangereous .Try to fgets() instead.
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).
C++ Syntax (Toggle Plain Text)
#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 lead.You may do m = x; } cout<<endl; } for(p = m; p <= x;p++) cout<<a[p]; return 0; }
And note that "gets()" function is dangereous .Try to fgets() instead.
Last edited by FoX_; Jun 14th, 2007 at 12:22 pm.
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!!
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!!
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 !!!
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.
•
•
Join Date: Oct 2006
Posts: 5
Reputation:
Solved Threads: 1
the below will reverse your sentence it works.
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main(){ char a[100]; char b[10][20]; int x,m = 0,p,wrd = 0, i = 0; cout<<"Enter a sentence"<<endl; gets(a); for(x=0; a[x] != '\0'; x++) { if(a[x] == ' ') { for(p=m;p <= x;p++) b[wrd][i++] = a[p]; b[wrd][i] = '\0'; m = x + 1; wrd++; i = 0; } } i=0; if(a[x] == '\0') for(p=m;p <= x;p++) b[wrd][i++] = a[p]; b[wrd][i] = '\0'; for(i=wrd; i >= 0 ; i--) { int j = 0; while( b[i][j] != '\0') cout << b[i][j++]; cout << endl; } }
![]() |
Other Threads in the C++ Forum
- Previous Thread: ofstream.. dynamic filename and path?
- Next Thread: scrabble!
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







