Picking A letter From a Char String

Started by
15 comments, last by Paradigm Shifter 10 years, 6 months ago

I am able to do this in 3 other Computer Languages, But I can not find the command to do it in C++.

Here it is :

char Alphabet[] = "ABCDEFGHIJK";

How do I get the 5th letter out of this string ? Keep in mind that at any given time the 5th letter may change ( I am not looking for the 'E")

Just the 5th or sometimes maybe the 8th letter.

The purpose is to add that letter to another string such as

strcat(Letter,<The 5th letter in the Alphabet

    >);

Plus any include files that may not be obvious.

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

Advertisement

Alphabet[4]

? (It's just an array! Because it is a string literal it is null terminated of course).

Or are you looking for the 5th unique letter from the string instead, that is more complicated (i.e. "AAAAABBBCCDDDEEEE" would want 'E' returned).

EDIT: Note you can't use a char as an argument to strcat though, you would need to make a null terminated char array, or do it by hand.

EDIT2: It's easy to make a null terminated char array out of it though:

char buff[2];

buff[0] = Alphabet[4];

buff[1] = '\0';

// strcat away using buff

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Since you're using C++, the correct solution is don't use char arrays. Use std::string.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Yeah, probably, depends what you're doing. (EDIT: e.g. having a char array alphabet[26] that doesn't change probably isn't shocking to a C++ programmer).

Using strcat is a sign you're not doing the correct thing though - a std::string knows it's length without having to scan through looking for a null terminator.

EDIT2:

std::string Alphabet = "ABCDEFGHIJK";

char fifthCharacter = Alphabet[4]; // fifthCharacter == 'E'

is still true though.

EDIT3:

std::string fifthCharacterAsString = Alphabet.substr(4, 1);

is probably what you want though.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Paradigm - Your response was spot on. Believe it or not, however, once I posted my question the way I did, the light bulb went on!! I went back to what I was doing, Looked at it, then wrote a small program , realizing it was an array of characters, and then added the added that character to my string as follows:

string = What;

What = "Option"

What = What+ Alphabet[4];

What Now equals "OptionE"

That did solve my problem. Prior to that I was using doing this

What = ("Option"+Alphabet[x]);

and getting a Type MisMatch type compile error. So my problem was solved.

Apoch, After reading yours I am still scratching my head.

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!


That did solve my problem. Prior to that I was using doing this

What = ("Option"+Alphabet[x]);

and getting a Type MisMatch type compile error. So my problem was solved.

What is 'What'?

What is a Variable Name to hold WhatEver I assign to it.

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

What is a Variable Name to hold WhatEver I assign to it.

I apologize for not being clear.

Which type of variable is What?


Which type of variable is What?

An std::string, with using namespace std; tacked on somewhere before.


Which type of variable is What?

An std::string, with using namespace std; tacked on somewhere before.

Thanks.

Your line of code is interesting to me because it doesn't look like there should be a compile error, just an annoying lack of compile error and some unexpected value in 'What'?


std::string What = ("Option"+Alphabet[x]);

"Option" is a string literal, which should behave a const char array.

Alphabet[x] should be a char.

Now, a char is just a 1-byte integer, and a char array also acts like a char pointer, so I'd expect pointer arithmetic out of this.

To confirm, I added this to a piece of code I had open:


        std::string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int x = 4;
        std::string What = ("Option"+Alphabet[x]);

There were no compile errors.

My expectation is that, because 'E' is some number beyond the string length of "Option", the assignment for string 'What' ends up filling itself from whatever happens to be in that memory location beyond "Option", until it hits a byte equal to 0.

Oh well, it feels like I'm probably just wasting your time. It's good you have everything working now.

This topic is closed to new replies.

Advertisement