Jump to content
Miek

"Breaking apart" a string in C++?

Recommended Posts

I've got to write a program that accepts a word from the user, assigns it to a string, and then converts it and outputs it in the ICAO format.

 

For example, if the user types in "OCC", then the output would say "Oscar Charlie Charlie".

 

I'm not sure how to "break apart" the string letter for letter so that I can output the appropriate word for each letter of the input.

Share this post


Link to post
Share on other sites

I've got to write a program that accepts a word from the user, assigns it to a string, and then converts it and outputs it in the ICAO format.

 

For example, if the user types in "OCC", then the output would say "Oscar Charlie Charlie".

 

I'm not sure how to "break apart" the string letter for letter so that I can output the appropriate word for each letter of the input.

 

Should be able to do a for loop going through the string, like so

 

for( int i; i < string.length(); i++)

char = string;

 

something along those lines...

Share this post


Link to post
Share on other sites

OK... When I get that to work, how can I make the program stop telling me I have a ton of uninitialized local variables? They all have to be assigned so that each letter will output the right word according to the ICAO, but they won't all necessarily be used.

 

As in, if the user types OCC, then the program will still say that everything but "o" and "c" are uninitialized local variables.

 

I find this question quite irritating.

Share this post


Link to post
Share on other sites

Well I know in java you can split a string something like string[] test = split("one two"," ")

 

so it will give you test[0] = one and test[1] = two so there should be something like that in C++

 

So I found this: http://dotnetperls.com/string-split scroll down to the separating words part.

 

That link is C# but it shouldn't be that much different

Edited by rmb938

Share this post


Link to post
Share on other sites

As I had to stuggle through learning to code myself, I'm not going to give you the answer, but give you the essential ideas:

 

1) You need to read the string one character at a time

2) The ASCII codes for letters a-z ascend in value, as do the values for A-Z (if you force cast a char to int, you get the value)

3) You can use toupper() to convert all letters to uppercase

4) Apply an offset to the ASCII value of a letter to make A equal to 0 (zero)

5) Hold all the words in a two dimensional array (an array of strings, aka an array of array of chars), assign

 

e.g.

 

char icao[26][];

icao[0][] = "Alpha";
icao[1][] = "Bravo";

 

6) If the character being read in the string isn't a letter (e.g. space, number), decide what to do instead of string concatenation (or successive string output)...

Share this post


Link to post
Share on other sites

I haven't really done anything with arrays yet. I don't even know how to use them...

Share this post


Link to post
Share on other sites

OK... When I get that to work, how can I make the program stop telling me I have a ton of uninitialized local variables? They all have to be assigned so that each letter will output the right word according to the ICAO, but they won't all necessarily be used.

 

As in, if the user types OCC, then the program will still say that everything but "o" and "c" are uninitialized local variables.

 

I find this question quite irritating.

 

To get rid of uninitialized variables, all you have to do is initialize them.

Share this post


Link to post
Share on other sites

Someone suggested to me to use stringname.at(number++) in a loop. What about that?

 

Also, forcing all the letters to the same case via tolower or toupper to prevent the program from getting confused?

 

AHA! TAKE THAT, C++! I have bent the program to my whim... With some help from a fellow classmate who is a genius at this. Tomorrow, she is getting a 20oz Starbucks beverage of her choice + any food item of her choice, as she said that's what she'd like.

 

I'd buy coffee for the people here, too, but... It's impossible. Sorry.

Edited by Miek

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...