string manipulation

J

Joe Nova

I'm a C++ noob and I need a little help manipulating strings.

I've got a program that takes an expression in the form:

"operand1 operator operand2"

I'd like to:

1. Find the total length of the string

2. Find the position of the operator (the operator will only be a '+'
or a '*')

3. Extract operand1 and put it into a new string

4. Extract operand2 and put it into a new string

5. Strip any zeroes from the beginning of these new strings.

Any help would be greatly appreciated!
 
G

Gavin Deane

Joe said:
I'm a C++ noob and I need a little help manipulating strings.

I've got a program that takes an expression in the form:

"operand1 operator operand2"

I'd like to:

1. Find the total length of the string

2. Find the position of the operator (the operator will only be a '+'
or a '*')

3. Extract operand1 and put it into a new string

4. Extract operand2 and put it into a new string

5. Strip any zeroes from the beginning of these new strings.

Any help would be greatly appreciated!

You're in luck. The C++ standard library provides a class called
std::string which can do everything you need. Reading up on the size
and find member functions is be a good place to start. The other member
functions available should be enough for you to answer all the
questions.

Gavin Deane
 
J

Joe Nova

I've looked at the string class and its methods, but I'm asking for
more actual implementation help.

- Joe
 
M

Mike Wahler

Joe Nova said:
I've looked at the string class and its methods, but I'm asking for
more actual implementation help.

- Joe

Pleast don't top-post. I've created a generic example program below.

#include <iostream>
#include <sstream>
#include <string>

int main()
{
/* create a string: */
std::string s("hello my world");

/* find the length of a string: */
std::string::size_type sz(s.size());
std::cout << "Length of the string \"" << s << "\" is " << sz << "\n\n";

/* find the first occurrence of a character in a string: */
char to_find('m');
std::string::size_type pos(s.find(to_find));
std::cout << "The character '" << to_find;

if(pos != std::string::npos)
std::cout << "' first occurs in the string \"" << s
<< "\" at position " << pos << "\n\n";
else
std::cout << "' not found in the string \"" << s << "\"\n\n";

/* extract white-space separated strings from a string and store
each in a separate string */

std::istringstream iss(s);
std::string one;
std::string two;
std::string three;

iss >> one >> two >> three;
std::cout << "\nWhitespace separated strings:\n"
<< "first string: \"" << one << "\"\n"
<< "second string: \"" << two << "\"\n"
<< "third string: \"" << three << "\"\n\n";

/* these strings won't contain any zeros unless you put them there,
so if you don't put any there, you won't need to remove them :) */

return 0;
}

-Mike
 
G

Gianni Mariani

Joe said:
I'm a C++ noob and I need a little help manipulating strings.

I've got a program that takes an expression in the form:

"operand1 operator operand2"

I'd like to:

1. Find the total length of the string

2. Find the position of the operator (the operator will only be a '+'
or a '*')

3. Extract operand1 and put it into a new string

4. Extract operand2 and put it into a new string

5. Strip any zeroes from the beginning of these new strings.

Any help would be greatly appreciated!


Take a look at "flex".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top