moonskyland
asked on
c++ substr
Hey,
i am doing one program, and i cant make program to take word from sentence.
I am doing like that :
if (s[i]= ' ' )
{
s1 = s.substr(0, i);
cout << s1 << "\n" ;
}
and it don't work, i declared "i" as int, i know it must be string, but when i declare as string, it even not starts.
How should i code at this place, let me know. thanks
i am doing one program, and i cant make program to take word from sentence.
I am doing like that :
if (s[i]= ' ' )
{
s1 = s.substr(0, i);
cout << s1 << "\n" ;
}
and it don't work, i declared "i" as int, i know it must be string, but when i declare as string, it even not starts.
How should i code at this place, let me know. thanks
You're in the Java area - do you realize?
Take a look at this reference page for substr :
http://www.cplusplus.com/reference/string/string/substr.html
You'll find an example of usage there.
I'm not 100% sure what you want to do, because you didn't show enough code ... Can you show a bit more ? Ie. the loop for i, the definition of s and s1, etc.
>> i declared "i" as int, i know it must be string
i has to be an integer value (size_t to be exact). int will work just fine.
http://www.cplusplus.com/reference/string/string/substr.html
You'll find an example of usage there.
I'm not 100% sure what you want to do, because you didn't show enough code ... Can you show a bit more ? Ie. the loop for i, the definition of s and s1, etc.
>> i declared "i" as int, i know it must be string
i has to be an integer value (size_t to be exact). int will work just fine.
But just it point out, == is the equality operator and = is the assignment operator
(So you first if statement is actually not doing a comparison, but making an assignment)
>>>>>>, i declared "i" as int, i know it must be string
no i has to be int only. Check out:
http://www.cppreference.com/cppstring/substr.html
what is the value of i at the time u are getting the substring out? it should be something greater than 0.
what is the value of s at that time?
try substr(0). it should give you the whole of the string s.
Hope it helps.
no i has to be int only. Check out:
http://www.cppreference.com/cppstring/substr.html
what is the value of i at the time u are getting the substring out? it should be something greater than 0.
what is the value of s at that time?
try substr(0). it should give you the whole of the string s.
Hope it helps.
ASKER
Here is my whole code
#include <iostream.h>
#include <cstdlib>
#include <string>
using namespace std;
int i;
char s[80];
int main ()
{
string s, s1;
cout << "Enter Sentence:\n";
getline(cin, s);
cout << "You entered:" << s << "\n";
if (s[i]= ' ' )
{
s1 = s.substr(0, i);
cout << s1 << "\n" ;
}
system("PAUSE");
}
#include <iostream.h>
#include <cstdlib>
#include <string>
using namespace std;
int i;
char s[80];
int main ()
{
string s, s1;
cout << "Enter Sentence:\n";
getline(cin, s);
cout << "You entered:" << s << "\n";
if (s[i]= ' ' )
{
s1 = s.substr(0, i);
cout << s1 << "\n" ;
}
system("PAUSE");
}
>> Here is my whole code
Ok. The code has a few problems ... First, i is 0 ... so s[i] will refer to the first character in the string - you'll have to create a loop to loop over the characters in the string until you find a ' ' (space). Then, what CEHJ already pointed out : you probebly don't want to do an assignment (=), but a comparison (==).
Give it a try, and post your modified code here ... and we'll help you further.
Ok. The code has a few problems ... First, i is 0 ... so s[i] will refer to the first character in the string - you'll have to create a loop to loop over the characters in the string until you find a ' ' (space). Then, what CEHJ already pointed out : you probebly don't want to do an assignment (=), but a comparison (==).
Give it a try, and post your modified code here ... and we'll help you further.
Oh, and don't use <iostream.h>. It's deprecated. Use <iostream> instead.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Error: 'i' has no value.
Error: if (s[i]= ' ' ) should be if (s[i]== ' ' ) as mentioned before.
;JOOP!
Error: if (s[i]= ' ' ) should be if (s[i]== ' ' ) as mentioned before.
;JOOP!
moonskyland, is this an assignment ? If so, then please ignore lucky_james's code, and try to find the solution yourself. You'll learn a lot more that way.
ASKER
yes, it is assignment. Yes, my mission is to learn, i wanna learn c++ :) And now i understand why it didn`t worked. Thanks all for help ;)
Did you at least take note of the other comments that were made ? Because there are still some issues with lucky_james's code.
It is also good EE etiquette to split points when several have helped. In case it's escaped your notice, moonskyland, i was the first to point out the main error in your code
ASKER
yeah CEHJ, i wish i could reopen this QUESTION, i would give you points also. And this code dont work correctly :(
lets say i write sentence "yesterday i ate food"
Here is output what i am getting:
WORD: yesterday
WORD: yesterday i
WORD: yesterday i ate
:(
lets say i write sentence "yesterday i ate food"
Here is output what i am getting:
WORD: yesterday
WORD: yesterday i
WORD: yesterday i ate
:(
That's because you've a logic error around space finding in the string, making it not possible for you to get the last token