473,396 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Aug 23 '05 #1
15 3172

"Jeannie" <Do**@orPepper.com> wrote in message
news:kk********************************@4ax.com...
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 <iostream>
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
Aug 23 '05 #2
Jeannie wrote:
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[i] << '\n';

return 0;
}

hello

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


Brian
Aug 23 '05 #3
On Tue, 23 Aug 2005 20:24:26 +1000, "benben" <moc.liamtoh@hgnohneb
read backward> wrote:

"Jeannie" <Do**@orPepper.com> wrote in message
news:kk********************************@4ax.com.. .
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 <iostream>
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!
Aug 23 '05 #4
On 23 Aug 2005 17:56:37 GMT, "Default User" <de***********@yahoo.com>
wrote:
Jeannie wrote:
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[i] << '\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!
Aug 23 '05 #5

Jeannie wrote:
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::ostream_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

Aug 24 '05 #6
On Tue, 23 Aug 2005 23:09:40 +0300, Jeannie <Do**@orPepper.com> wrote:

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>>
Aug 24 '05 #7
On 23 Aug 2005 20:43:02 -0700, "Chris ( Val )"
<ch******@bigpond.com.au> wrote:
# 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::ostream_iterator<char>( std::cout, "\n" ) );

return 0;
}

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

J
Aug 24 '05 #8
Chris ( Val ) wrote:

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:


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::ostream_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
Aug 24 '05 #9
> 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.

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

Aug 25 '05 #10

"Jeannie" <Do**@orPepper.com> wrote in message news:kk********************************@4ax.com...
[snip]
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...49a1be3a5c6335
http://groups.google.com/group/perfo...c775cf7e3cdcf0
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Aug 25 '05 #11
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::ostream_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

Aug 25 '05 #12

Default User wrote:
Chris ( Val ) wrote:

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:


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::ostream_iterator<char>( std::cout, "\n" ) );

return 0;
}

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
Brian


Aug 25 '05 #13

an****************@gmail.com wrote:
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::ostream_iterator<char>( std::cout, "\n" ) );


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

Aug 25 '05 #14

Jeannie wrote:

#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

Aug 27 '05 #15
On Tue, 23 Aug 2005 13:04:50 +0300, Jeannie <Do**@orPepper.com> wrote:
Hello group! -- deleted to thwart net Nazi attacks -- Jeannie


I was able to complete my task with the early suggestions. Thank you
all for being so helpful!

J
Aug 30 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cavello | last post by:
Hi, I'm curious as to how I can input a string of characters and then output it in sections, seperating these sections according to a particular value entered, for example a period. Say if I enter...
5
by: derrick | last post by:
I used to program text RPG's in C back in high school, but I was very crude at it and only learned what I needed to get the job done. I am now about to graduate college with a degree in English and...
4
by: Merlin | last post by:
Hi there, I would like to check if a string is a valid zip code via Javascript. Length and existents are already checked. How can I find out if the string contains characters other than...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
3
by: SharpCoderMP | last post by:
i've run into some trouble using data from xml inside my app. the scenario is simple. input data looks more or less like this: <item> <name>MyName</name> <somefloat>11.5</somefloat> </item> ...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
0
by: shiva_seshadri | last post by:
Hi, I want to parse non-printable characters in Visual Basic. Is there any custom method available in Visual Basic. Could anyone please help Thanks and Regards, Shiva.
4
by: vinothkumar86 | last post by:
How to split a particular word in to individual characters??? eg: vino as v i n o ...
3
by: Simon Pickles | last post by:
Hi Can anyone suggest a really simple XML reader for python? I just want to be able to do something like this: xmlDoc = xml.open("file.xml") element = xmlDoc.GetElement("foo/bar") .... to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.