[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

c++ substr

Posted on 2007-10-21
16
Medium Priority
?
4,759 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:moonskyland
  • 5
  • 5
  • 3
  • +2
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20117463
You're in the Java area - do you realize?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20117465
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20117466
But just it point out, == is the equality operator and = is the assignment operator
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:CEHJ
ID: 20117468
(So you first if statement is actually not doing a comparison, but making an assignment)
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20117470
>>>>>>, 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.
0
 

Author Comment

by:moonskyland
ID: 20117471
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");

    }

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20117473
>> 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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20117476
Oh, and don't use <iostream.h>. It's deprecated. Use <iostream> instead.
0
 
LVL 7

Accepted Solution

by:
lucky_james earned 500 total points
ID: 20117479
1. make '=' to '=='
This would enable you to make substring only when character is ' ', not even in the first time.
Your code is corrupting string s.

2. where is the loop.
put a loop to iterate thru string s.

try following 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";
for(int i = 0; i<s.length();i++)
{
    if (s[i]== ' ' )
    {
    s1 =  s.substr(0, i);
    cout << s1 << "\n" ;
    }
   
 }
   
    system("PAUSE");

    }
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20117480
Error: 'i' has no value.
Error: if (s[i]= ' ' ) should be if (s[i]== ' ' ) as mentioned before.

;JOOP!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20117485
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.
0
 

Author Comment

by:moonskyland
ID: 20117561
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 ;)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20117580
Did you at least take note of the other comments that were made ? Because there are still some issues with lucky_james's code.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20117607
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
0
 

Author Comment

by:moonskyland
ID: 20118179
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

:(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20118204
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month19 days, 2 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question