C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old February 25th, 2003, 03:23 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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...

Reply With Quote
  #2  
Old February 25th, 2003, 03:32 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
Use strchr() or strstr()

Reply With Quote
  #3  
Old February 25th, 2003, 03:55 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
hmm not formiliar with those..ill do some research. Thanks!

-andy

Reply With Quote
  #4  
Old February 25th, 2003, 04:48 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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;
	}
}

Reply With Quote
  #5  
Old February 25th, 2003, 06:29 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
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

Reply With Quote
  #6  
Old February 25th, 2003, 07:29 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
hmm..both interesting ways..I understand 7stud more, but thanks for your reply vpopper!

-andy

Reply With Quote
  #7  
Old February 25th, 2003, 07:43 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
what if you wanted to text a rage of letters, as A through F.

Reply With Quote
  #8  
Old February 25th, 2003, 08:48 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #9  
Old February 25th, 2003, 09:37 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
got it to work..thanks man.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > string letter detection?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway