Picking A letter From a Char String

This topic is 1559 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[list]>);

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.

Share on other sites

String literals do not respond to addition in the way you're expecting. That will take the address of the string literal, interpret it as a char pointer, then add the value of the char you're getting from Alphabet[x], which will be in the 0x41-0x5A range. (Edit - Nevermind, it looks like you realized that.)

If you want to append to a std::string then you can use the append() method, though it doesn't support single char as input. You need to pass it a std::string or a C-style string, so we've more or less come full circle here.

You could do this:

#include <string>
#include <iostream>

int main() {
using namespace std;
string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string str = "Option";
int x = 4;
char temp[2] = {Alphabet[x], 0}; //kludge together a C string from two char values
str.append(temp);
cout << str << endl;

system("pause");
return 0;
}


Edited by Khatharr

Share on other sites

If you want to append to a std::string then you can use the append() method, though it doesn't support single char as input. You need to pass it a std::string or a C-style string, so we've more or less come full circle here.

You can append a single char N times.

std::string What = "Option";
What.append(1, Alphabet[x]);


Share on other sites

If you want to append to a std::string then you can use the append() method, though it doesn't support single char as input. You need to pass it a std::string or a C-style string, so we've more or less come full circle here.

You can append a single char N times.

std::string What = "Option";
What.append(1, Alphabet[x]);

You can also push_back a single char:

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

Share on other sites

In Visual C++ you may declare:

CString Alphabet = "ABCDEFGHIJK";

the 5th letter out of this string:

CString c5 = Alphabet.Mid(4,1);

Share on other sites

In Visual C++ you may declare:
CString [background=#fafbfc]Alphabet = "ABCDEFGHIJK";[/size][/background]

[background=#fafbfc]the 5th letter out of this string:[/size][/background]
CString c5 = [background=#fafbfc]Alphabet.Mid(4,1);[/size][/background]

That is plain horrible advice. There is C++ standard library class which does the same job (only apparently better if Mid(4, 1) is indeed the way to retrieve the character with index 4). CString is useless if you have the intention of writing standard compliant code and perhaps port the program to a different platform. Even when using MSVC only you are pulling in a rather large dependency you really do not need under normal circumstances. Edited by BitMaster

Share on other sites

Some day I'll actually just sit down and comprehensively read a set of STL docs.

Share on other sites

In Visual C++ you may declare:
CString Alphabet = "ABCDEFGHIJK";[/size]

the 5th letter out of this string:[/size]
CString c5 = Alphabet.Mid(4,1);[/size]

That is plain horrible advice. There is C++ standard library class which does the same job (only apparently better if Mid(4, 1) is indeed the way to retrieve the character with index 4). CString is useless if you have the intention of writing standard compliant code and perhaps port the program to a different platform. Even when using MSVC only you are pulling in a rather large dependency you really do not need under normal circumstances.

Well I already mentioned the C++ equivalent which is Alphabet.substr(4,1);

I agree don't pull in ATL or worse MFC if you can use the standard library for the job.