Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

A String Array Search Function

string function array search string array

  • Please log in to reply
4 replies to this topic

#1 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 13 July 2012 - 06:35 AM

Hi I tried to write a function which searches for a string in a string array. It returns false if it finds and true if it doesn't. Can you tell me that if it is true or not? I couldn't make it work on my compiler..

bool check(var1, var2[])
{
int i;
for(i=0;i<30;i++)
{
if (var1==var2[i])
return false;
break;
}
return true;
}
thanks
  • 0

#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 13 July 2012 - 07:55 AM

mmm, does it work withouth the char * in the parameter declaration ?

this comparison var1==var2[i] would like comparing if the memory address is the same,
you should compare char by char or use some memcmp or strcmp function

oh, the for loop executes just one time, since you put a break on it ( outside if's scope )
  • 0

#3 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 13 July 2012 - 11:34 PM

In C you'll have to use strcmp function, in C++ you can use == operator (if using std::string).

Edit: misread the question entirely.
  • 0

The roots of education are bitter, but the fruit is sweet.


#4 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 15 July 2012 - 10:18 PM

bool check(char* var1,char* var2[])
{
int i;
for(i=0;i<30;i++)
{
if (strcmp(var1,var2[i]))
return false;
break;
}
return true;
}
is it true now? is it correct go pass string array like this?
char* var2[]
or should I write like this?
string var1, string var2[]
thanks
  • 0

#5 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 16 July 2012 - 12:20 AM

strcmp returns 0 if the strings are equal, a positive number if the first string is greater, and a negative number if the second string is greater. So really you need to change your if statement to check for a return value of 0. If you're using plain char * strings, use strcmp. If you're using C++ strings, use the == operator.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download