The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
string letter detection?
Discuss string letter detection? in the C Programming forum on Dev Shed. string letter detection? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 25th, 2003, 03:23 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
string letter detection?
Is there a way that I can tell if a string has a certain letter in it? For example:
string asdfa;
cout<<"type in string";
cin>>asdfa;
if...(there is the letter 'a' in string)
{
cout << "Your string contained the letter 'a' which isn't allowed in my program";
}
else if (if there isn't letter 'a' in string)
{
continue...
}
So I guess I am askign if there is a way I can detect a letter in a string and react based on if the user has typed that letter or not. Thanks!
-andy
__________________
hmmm...
|

February 25th, 2003, 03:32 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Use strchr() or strstr()
|

February 25th, 2003, 03:55 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
hmm not formiliar with those..ill do some research. Thanks!
-andy
|

February 25th, 2003, 04:48 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
andy3109,
Those functions are going to implement a process whereby you examine each character in the string using array notation. Using the string length as the loop control, you can examine each character of the string like this:
Code:
string text;
char looking_for = 'a';
bool found_flag = false;
for(int i=0; i<text.length(); i++)
{
if(text[i]==looking_for)
{
found_flag=true;
break;
}
}
|

February 25th, 2003, 06:29 PM
|
 |
Contributing User
|
|
Join Date: Jun 2000
Location: Southern California
Posts: 73
Time spent in forums: 56 m 9 sec
Reputation Power: 13
|
|
Use the STL:
Code:
#include <algorithm>
std::string::size_type pos =0;
pos = asdfa.find_first_of("x", 0); // find "x"
if (pos == std::string::npos)
// nope
else
// found
|

February 25th, 2003, 07:29 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
hmm..both interesting ways..I understand 7stud more, but thanks for your reply vpopper!
-andy
|

February 25th, 2003, 07:43 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
what if you wanted to text a rage of letters, as A through F.
|

February 25th, 2003, 08:48 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
All characters are stored as integer codes. You can look up the ASCII codes for all the characters--there's a table of codes in the appendix of my book, so check yours for the table.
ASCII codes for A-F are 65-70, so you can test whether text[i] is between 65 and 70.
Last edited by 7stud : February 25th, 2003 at 08:52 PM.
|

February 25th, 2003, 09:37 PM
|
 |
Contributing User
|
|
Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
|
|
|
got it to work..thanks man.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|