|
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 | |
Share:
|
"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 | | |
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 | | |
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! | | |
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! | | |
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 | | |
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>> | | |
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 | | |
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 | | |
> 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 | | |
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 | | |
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 | | | 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 | | |
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 | | |
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 | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by Cavello |
last post: by
|
5 posts
views
Thread by derrick |
last post: by
|
4 posts
views
Thread by Merlin |
last post: by
|
9 posts
views
Thread by Python.LeoJay |
last post: by
|
3 posts
views
Thread by SharpCoderMP |
last post: by
|
6 posts
views
Thread by trevor |
last post: by
| | |
3 posts
views
Thread by Simon Pickles |
last post: by
| | | | | | | | | | |