snakey Posted January 9, 2008 Posted January 9, 2008 How would i select a letter in a string and make it its own string? So say if i wanted to use the letter "a" in Snakey and make it its own string or even just have an if statement that uses the letter you select. Quote
jollyrancher82 Posted January 9, 2008 Posted January 9, 2008 char* string = "snakey"; if (string[2] == 'a') { // foo... } Quote
snakey Posted January 16, 2008 Author Posted January 16, 2008 Hmmm not really what im after say i have "string x" and x=snakey how do i use the a as a seperate string. So i'll have int x = snakey then i'll make int y which will = a. P.S. i dont no what x is at the start it gets entered as an input and i use each letter individually if that helps any. Quote
SomeoneE1se Posted January 16, 2008 Posted January 16, 2008 if you use char* x = "snakey"; x[0] will be s x[1] will be n x[2] will be a x[3] will be k x[4] will be e x[5] will be y is that what you mean? Quote
trustme Posted January 17, 2008 Posted January 17, 2008 I'm sure there's a built in function for it, I have done it in Java, but not C++, sorry. string str1( "Alpha Beta Gamma Delta" ); unsigned int loc = str1.find( "Omega", 0 ); if( loc != string::npos ) cout << "Found Omega at " << loc << endl; else cout << "Didn't find Omega" << endl; That does what you want on the side... just change the "Omega" to the character, and use the character position that is returned to substring(Java) whatever you want out. Read under the "find" command here: http://www-control.eng.cam.ac.uk/~pcr20/ww...ng_details.html Quote
snakey Posted January 17, 2008 Author Posted January 17, 2008 SomeoneE1se has done it thanks man Quote
Anthrax Posted January 17, 2008 Posted January 17, 2008 easier way would be to do a loop then you could loop through for any thing. something like: while stringx != '0'{ if stringx = x strcpy( stringx, newString); i++; } just off the top of my head, this wont work of course but it gives you an idea. Quote
SomeoneE1se Posted January 17, 2008 Posted January 17, 2008 what does that do I know I just woke up but HUH? Quote
jollyrancher82 Posted January 19, 2008 Posted January 19, 2008 easier way would be to do a loop then you could loop through for any thing. something like: while stringx != '0'{ if stringx = x strcpy( stringx, newString); i++; } just off the top of my head, this wont work of course but it gives you an idea. Wow, just wow... that's so wrong. Quote
hexlax Posted January 23, 2008 Posted January 23, 2008 #include <string> using namespace std; string word1 = "snakey"; string word2 = ""; for(int x=0; x<word1.length(); x++) if(word1[ x ] == 'a') word2 = word1[ x ]; Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.