| | |
How to extract words from char type string
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
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.
•
•
Join Date: Oct 2006
Posts: 5
Reputation:
Solved Threads: 1
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'
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
hope you get the idea. just throw in some cout statments in the
code and print b[i][j] you will get it.
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'
C++ Syntax (Toggle Plain Text)
i=0; if(a[x] == '\0') for(p=m;p <= x;p++) b[wrd][i++] = a[p]; 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
C++ Syntax (Toggle Plain Text)
for(i=wrd; i >= 0 ; i--) { int j = 0; while( b[i][j] != '\0') cout << b[i][j++]; cout << endl; }
hope you get the idea. just throw in some cout statments in the
code and print b[i][j] you will get it.
•
•
•
•
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; } }
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*
>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:
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.
>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:
C++ Syntax (Toggle Plain Text)
#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'; }
I'm here to prove you wrong.
[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 !!!
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.
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?
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?
>-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:
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:
>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:
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:
C++ Syntax (Toggle Plain Text)
#include <iomanip> ... while ( i < 10 && cin>> setw ( 100 ) >> words[i] )
>-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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; int main() { const int size = 100; int n; cout<<"Number of words: "; if ( cin>> n ) { char (*words)[size] = new char[n][size]; cout<<"Enter a sentence: "; int i = 0; while ( i < n && cin>> setw ( size ) >> words[i] ) ++i; while ( --i >= 0 ) cout<< words[i] <<' '; cout<<'\n'; // Always release what you allocate delete[] words; } }
>"n-size".Its dynamic allocation right?
Yes. It goes like this:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; int main() { // Use the new[n] operator to // allocate memory to a pointer char *p = new char[100]; // Use it pretty much like an array if ( cin.getline ( p, 100 ) ) cout<<">"<< p <<"<\n"; // Use the delete[] operator to // release memory allocated with new[n] delete[] p; }
I'm here to prove you wrong.
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
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
>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.
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: ofstream.. dynamic filename and path?
- Next Thread: scrabble!
| Thread Tools | Search this Thread |
api array arrays 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 download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






