Picking A letter From a Char String

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

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;
}

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement


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]);

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]);
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

In Visual C++ you may declare:

CString Alphabet = "ABCDEFGHIJK";

the 5th letter out of this string:

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

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

[background=#fafbfc]the 5th letter out of this string:[/background]
CString c5 = [background=#fafbfc]Alphabet.Mid(4,1);[/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.

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

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

In Visual C++ you may declare:
CString Alphabet = "ABCDEFGHIJK";

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

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.

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

This topic is closed to new replies.

Advertisement