Raised This Month: $51 Target: $400
 12% 

Check char in a string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
killergirl
Senior Member
Join Date: Jul 2010
Old 04-03-2012 , 03:16   Check char in a string
Reply With Quote #1

I have a string sz[] = "abcdefghijklmnopqrst", and how can I find a single char in this string? containi(), equal() or ? What am I suposed to do?

Thanks !
killergirl is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 04-03-2012 , 03:24   Re: Check char in a string
Reply With Quote #2

PHP Code:
/*
  new bool: bContain = StringContainChar( "abcdefghijklmnopqrstu", 'k' );

  bContain will be true because 'k' exists in string.
*/

StringContainCharcLine[ ], iChar )
{
  static 
i;

  for( 
0strlencLine ); i++ )
    if( 
cLine] == iChar )
      return 
true;

  return 
false;


Last edited by claudiuhks; 04-03-2012 at 03:27.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-03-2012 , 09:55   Re: Check char in a string
Reply With Quote #3

Code:
containchar(const haystack[], needle, bool:case_sensitive=true) {     new needleString[2];     needleString[0] = needle;         return strfind(haystack, needleString, _:case_sensitive); }
Exolent[jNr] is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-03-2012 , 20:49   Re: Check char in a string
Reply With Quote #4

strfind - Finds a string in another string. Returns -1 if not found.

Code:
containchar(const haystack[], needle, bool:case_sensitive=true) {     new needleString[2];     needleString[0] = needle;    
    return (strfind(haystack, needleString, _:case_sensitive) > -1);
}
or
Code:
containchar(const haystack[], const needle[], bool:case_sensitive=true) {     return (strfind(haystack, needle, _:case_sensitive) > -1); }
__________________

Last edited by Bugsy; 04-03-2012 at 20:52.
Bugsy is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-03-2012 , 21:28   Re: Check char in a string
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
strfind - Finds a string in another string. Returns -1 if not found.
Right. I was mimicking the contain() functionality but didn't want to make 2 functions for insensitive.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-03-2012 , 23:34   Re: Check char in a string
Reply With Quote #6

My point was you are checking the return value of strfind() to see if needle was found in haystack, it would always return true EXCEPT if the needle was the first character in haystack. You need to include the > -1 condition, which would also be needed with contain[i], they also return -1 if nothing found.
__________________
Bugsy is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-03-2012 , 23:39   Re: Check char in a string
Reply With Quote #7

Quote:
Originally Posted by Bugsy View Post
it would always return true EXCEPT if the needle was the first character
That's based on how the function was used.

If used like
Code:
if(containchar("abcdefg", 'a')) {     // }
Then you would be right. However, I was assuming the author would know how to use functions which return the index since contain() was mentioned in the first post.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-03-2012 , 23:46   Re: Check char in a string
Reply With Quote #8

Alright, I understand your intention now..and figured you knew how strfind() worked.
__________________
Bugsy is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 04-04-2012 , 00:14   Re: Check char in a string
Reply With Quote #9

If it was purely a string containing flags (a-z) would it be better to convert to a bitsum and use the bit and operator?
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-04-2012 , 00:31   Re: Check char in a string
Reply With Quote #10

Quote:
Originally Posted by Emp` View Post
If it was purely a string containing flags (a-z) would it be better to convert to a bitsum and use the bit and operator?
Unsure of the performance difference between read_flags() and strfind().
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Reply



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:46.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode