Jump to content

  • Log In with Google      Sign In   
  • Create Account


Picking A letter From a Char String


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 Poigahn   Crossbones+   -  Reputation: 517

Like
0Likes
Like

Posted 11 October 2013 - 06:10 PM

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.


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


Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5213

Like
2Likes
Like

Posted 11 October 2013 - 06:12 PM

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


Edited by Paradigm Shifter, 11 October 2013 - 07:05 PM.

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

#3 ApochPiQ   Moderators   -  Reputation: 14623

Like
7Likes
Like

Posted 11 October 2013 - 07:14 PM

Since you're using C++, the correct solution is don't use char arrays. Use std::string.

#4 Paradigm Shifter   Crossbones+   -  Reputation: 5213

Like
0Likes
Like

Posted 11 October 2013 - 07:35 PM

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.


Edited by Paradigm Shifter, 11 October 2013 - 07:57 PM.

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

#5 Poigahn   Crossbones+   -  Reputation: 517

Like
0Likes
Like

Posted 12 October 2013 - 03:53 AM

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 !!


#6 Pink Horror   Members   -  Reputation: 1115

Like
0Likes
Like

Posted 12 October 2013 - 01:44 PM


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



#7 Poigahn   Crossbones+   -  Reputation: 517

Like
0Likes
Like

Posted 12 October 2013 - 04:41 PM

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


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


#8 Pink Horror   Members   -  Reputation: 1115

Like
0Likes
Like

Posted 12 October 2013 - 08:01 PM

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

 

I apologize for not being clear.

 

Which type of variable is What?



#9 Ectara   Crossbones+   -  Reputation: 2873

Like
0Likes
Like

Posted 12 October 2013 - 08:11 PM


Which type of variable is What?

 

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



#10 Pink Horror   Members   -  Reputation: 1115

Like
0Likes
Like

Posted 12 October 2013 - 08:35 PM

 


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.



#11 Khatharr   Crossbones+   -  Reputation: 2914

Like
0Likes
Like

Posted 12 October 2013 - 08:54 PM

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, 12 October 2013 - 09:00 PM.

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.

#12 Pink Horror   Members   -  Reputation: 1115

Like
1Likes
Like

Posted 13 October 2013 - 09:21 AM


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


#13 Cornstalks   Crossbones+   -  Reputation: 6974

Like
3Likes
Like

Posted 13 October 2013 - 09:33 AM

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

[ 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 ]

#14 Petrov_VA   Members   -  Reputation: 592

Like
-2Likes
Like

Posted 13 October 2013 - 11:57 PM

In Visual C++ you may declare:

CString Alphabet = "ABCDEFGHIJK";

 

the 5th letter out of this string:

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



#15 BitMaster   Crossbones+   -  Reputation: 3771

Like
1Likes
Like

Posted 14 October 2013 - 01:21 AM

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.

Edited by BitMaster, 14 October 2013 - 01:24 AM.


#16 Khatharr   Crossbones+   -  Reputation: 2914

Like
0Likes
Like

Posted 14 October 2013 - 08:59 AM

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.

#17 Paradigm Shifter   Crossbones+   -  Reputation: 5213

Like
0Likes
Like

Posted 14 October 2013 - 01:07 PM

 

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.


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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS