Public Group

# Picking A letter From a Char String

This topic is 2108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites

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

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

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'?

##### Share on other sites

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

##### Share on other sites

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

I apologize for not being clear.

Which type of variable is What?

##### Share on other sites

Which type of variable is What?

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

##### Share on other sites

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.

• 13
• 18
• 29
• 11