How do I parse a string into individual characters? (really simple!) really!

J

Jeannie

Hello group!

I'm in Europe, traveling with my laptop, and I don't any compilers
other than Borland C++ 5.5. available. I also don't have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don't remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it's
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.

Thanks a million!

Jeannie
 
B

benben

Jeannie said:
Hello group!

I'm in Europe, traveling with my laptop, and I don't any compilers
other than Borland C++ 5.5. available. I also don't have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don't remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it's
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>

Should be said:
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

In C++, arrays are zero-based, you start with vec[0].
I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.

Thanks a million!

Jeannie

Whatever, there's the standard library to do all these for you:

#include <iostream>
#include <string>

using namespace std;

string s;
getline(cin, s);

// now if you input "Good" then
// s[0]='G', s[1]='o', s[2]='o', s[3]='d'

Ben
 
D

Default User

Jeannie said:
Hello group!

I'm in Europe, traveling with my laptop, and I don't any compilers
other than Borland C++ 5.5. available. I also don't have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don't remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it's
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.


That's not a good way to do what you want. What exactly do you want to
do with this vector besides find the length of the string?

My suggestion would be:

#include <string>
#include <iostream>

// less typing for example
using namespace std;

int main()
{
string MyString;

getline(cin, MyString);

cout << "Length = " << MyString.length() << '\n';

for (int i = 0; i < MyString.length(); i++)
cout << "MyString[" << i << "] " << MyString << '\n';

return 0;
}

hello

Length = 5
MyString[0] h
MyString[1] e
MyString[2] l
MyString[3] l
MyString[4] o




Brian
 
J

Jeannie

Jeannie said:
Hello group!

I'm in Europe, traveling with my laptop, and I don't any compilers
other than Borland C++ 5.5. available. I also don't have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don't remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it's
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>

Should be said:
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

In C++, arrays are zero-based, you start with vec[0].
I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.

Thanks a million!

Jeannie

Whatever, there's the standard library to do all these for you:

#include <iostream>
#include <string>

using namespace std;

string s;
getline(cin, s);

// now if you input "Good" then
// s[0]='G', s[1]='o', s[2]='o', s[3]='d'

Ben


Ben, this looks great, thanks!
 
J

Jeannie

Jeannie said:
Hello group!

I'm in Europe, traveling with my laptop, and I don't any compilers
other than Borland C++ 5.5. available. I also don't have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don't remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it's
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.


That's not a good way to do what you want. What exactly do you want to
do with this vector besides find the length of the string?

My suggestion would be:

#include <string>
#include <iostream>

// less typing for example
using namespace std;

int main()
{
string MyString;

getline(cin, MyString);

cout << "Length = " << MyString.length() << '\n';

for (int i = 0; i < MyString.length(); i++)
cout << "MyString[" << i << "] " << MyString << '\n';

return 0;
}

hello

Length = 5
MyString[0] h
MyString[1] e
MyString[2] l
MyString[3] l
MyString[4] o


Brian


Brian, this looks great, thanks!
 
C

Chris ( Val )

Jeannie said:
Hello group!

I'm in Europe, traveling with my laptop, and I don't any compilers
other than Borland C++ 5.5. available. I also don't have any manuals
or help files available. Sadly, more crippling problems! I never used
C++ much, so, I don't remember commands or fuctions well. My questions
are simple. I posted the DOS window output so you can see the Borland
C++ version I am using:

E:\TZ>bcc32 file2
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
File2.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

What are the commands or functions needed to parse a string into it's
individual characters?

Example:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.

Thanks a million!

Jeannie,

Since you are using C++ (and already have some answers),
I'll give you an alternative that makes use of the modern
standard library features:

# include <iostream>
# include <ostream>
# include <string>
# include <vector>
# include <algorithm>

int main()
{
std::string Buffer;

std::cout << "Please enter a string: ";
std::getline( std::cin, Buffer );

// copies each character of 'Buffer' into the vector
std::vector<char> V( Buffer.begin(), Buffer.end() );

// Display each character stored in the vector
std::copy( Buffer.begin(), Buffer.end(),
std::eek:stream_iterator<char>( std::cout, "\n" ) );

return 0;
}

The Standard C++ Library has a rich set of tools to help you
achieve your goals in the shortest possible time, so it is a
good to start getting familiar with them :)

HTH.

Cheers,
Chris Val
 
B

Barry Schwarz

snip 80+ lines
Brian, this looks great, thanks!

Would you please trim your posts. There is no need to quote
everything just to say thanks.


<<Remove the del for email>>
 
J

Jeannie

# include <iostream>
# include <ostream>
# include <string>
# include <vector>
# include <algorithm>

int main()
{
std::string Buffer;

std::cout << "Please enter a string: ";
std::getline( std::cin, Buffer );

// copies each character of 'Buffer' into the vector
std::vector<char> V( Buffer.begin(), Buffer.end() );

// Display each character stored in the vector
std::copy( Buffer.begin(), Buffer.end(),
std::eek:stream_iterator<char>( std::cout, "\n" ) );

return 0;
}


Thank you, I'll give this a try as well as the other suggestions.

J
 
D

Default User

Chris ( Val ) wrote:

Jeannie,

Since you are using C++ (and already have some answers),
I'll give you an alternative that makes use of the modern
standard library features:

She already had one of those.
# include <iostream>
# include <ostream>
# include <string>
# include <vector>
# include <algorithm>

int main()
{
std::string Buffer;

std::cout << "Please enter a string: ";
std::getline( std::cin, Buffer );

// copies each character of 'Buffer' into the vector
std::vector<char> V( Buffer.begin(), Buffer.end() );

// Display each character stored in the vector
std::copy( Buffer.begin(), Buffer.end(),
std::eek:stream_iterator<char>( std::cout, "\n" ) );

return 0;
}


What advantages does a vector of char have over the string you already
created? Yes, I know she mentioned a vector in her problem
decscription, but then she really was looking for design help.

Reading into a string, then converting to a vector is in my mind a
complete waste of time, unless some overriding reason that has not been
specified is present.

Her requirements were to be able to read in character data into a
container, get the size, and loop through to the end. As my example
showed, std::string already has all that.




Brian
 
B

benben

What advantages does a vector of char have over the string you already
created? Yes, I know she mentioned a vector in her problem
decscription, but then she really was looking for design help.

Reading into a string, then converting to a vector is in my mind a
complete waste of time, unless some overriding reason that has not been
specified is present.

It depends. A vector guarantees continueous memory block, but a string
doesn't.
 
A

Alex Vinokur

Jeannie said:
If I have a string input from the keyboard as: Good Morning!
I want a variable, maybe a vector, such as
vec[1]=G, vec[2]=o, vec[3]=o, ... vec[12]=g, vec[13]=!

I will need to determine the length of the string in bytes, and then
move through the string to the end. How do I declare a character
vector variable called "vec" here, and determine the length? Then I
can loop through until the end.
[snip]

See "Simple C/C++ Perfometer: Splitting string into vector of vectors":

http://groups.google.com/group/perfo/msg/9d49a1be3a5c6335
http://groups.google.com/group/perfo/msg/f3c775cf7e3cdcf0
 
A

annamalai.gurusami

Chris ( Val ) wrote:

(snip)
# include <iostream>
# include <ostream>
# include <string>
# include <vector>
# include <algorithm>

#include <iterator>

(snip)
// Display each character stored in the vector
std::copy( Buffer.begin(), Buffer.end(),
std::eek:stream_iterator<char>( std::cout, "\n" ) );

Just one point to add to this. The ostream_iterator is available
in the "iterator" header file.

For more information,

http://www.sgi.com/tech/stl/ostream_iterator.html

Rgds,
anna
 
C

Chris ( Val )

Default said:
Chris ( Val ) wrote:



She already had one of those.



What advantages does a vector of char have over the string you already
created?

Well, one poster has already mentioned one possible advantage,
but note that I did not post my code example to demonstrate a
comparison between std::string and std::vector.
Yes, I know she mentioned a vector in her problem
decscription, but then she really was looking for design help.

Well then you have (sort of), answered your own question :)

What aspect of what I posted do you feel is so wrong that it
warrants questioning?
Reading into a string, then converting to a vector is in my mind a
complete waste of time, unless some overriding reason that has not been
specified is present.

Whether it is a complete waste of time or not is not up to me
to decide really - I just provided what the OP was looking for
with a modern approach.
Her requirements were to be able to read in character data into a
container

True, and the container wanted was a vector<char>().

QUOTE:
"How do I declare a character vector variable called "vec" here"

, get the size, and loop through to the end. As my example
showed, std::string already has all that.

And the vector<char> (what the OP wanted) also has all of that.

Like I said, I just provided what the OP was looking for with
a modern approach, and in particular I wanted to demonstrate
how to initialise the vector with the given sequence of
characters from a std::string - Note that there is no need
for looping to do that.

I appreciate your comments, though I don't really see the need
for them :) Did you perhaps missunderstand what I was trying to
demonstrate?

I'm not always the best of writers (especially when in a hurry),
that's for sure <G>.

Cheers,
Chris Val
 
C

Chris ( Val )

Chris ( Val ) wrote:

(snip)


#include <iterator>

(snip)


Just one point to add to this. The ostream_iterator is available
in the "iterator" header file.

Indeed it is.

My compiler (Broland C++ Builder 5.0) lets me get away
with things like this, so I often forget to include it
when I'm in a hurry.

Thank you for the correction :)

Cheers,
Chris Val
 
A

Anand Hariharan

Jeannie said:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream.h>
int main(void)
{ char MyString[80] ;
printf("Input a string: ");
gets(MyString);
printf("The string input was: %s\n", MyString);
return 0;
}

While many have given you C++ solutions for what you sought, I have one
comment on your C-ish effort. Forget 'gets'. Use 'fgets' instead.

- Anand
 

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
473,731
Messages
2,569,432
Members
44,836
Latest member
BuyBlissBitesCBD

Latest Threads

Top