Problem with string search of an array

Reply

Join Date: Jan 2005
Posts: 2
Reputation: cricket is an unknown quantity at this point 
Solved Threads: 0
cricket cricket is offline Offline
Newbie Poster

Problem with string search of an array

 
0
  #1
Jan 30th, 2005
I am just learning C/C++. My problem is to determine if a character is or isn't within an array then returning an appropriate message to the user. I've only gotten this far using 'strchr' without much success. Any suggestions or help would be appreciated. Should I be using pointers instead within the function?

Thank you

Program Char Search */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char check(char c, char str);

main()

{ char response;
char strg[] = "Array test";
printf("%s\n", strg);

{ Printf("Enter the character you would like to search for.");

Scanf(" %c\n", &response);
}

/*function 'check'*/
{
char check(char str[],char c);
str = strg;
c = response;
scanf("strchr: %s\n",&strg);

if strchr=c
printf("The character is %s\n",c);
}





return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 45
Reputation: Siersan is an unknown quantity at this point 
Solved Threads: 2
Siersan's Avatar
Siersan Siersan is offline Offline
Speaker of Truth

Re: Problem with string search of an array

 
0
  #2
Jan 30th, 2005
Using strchr it would look something like this.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int check(char str[], int search);
  6.  
  7. int main(void)
  8. {
  9. char buf[BUFSIZ];
  10. int ch;
  11.  
  12. printf("Enter a string: ");
  13. fflush(stdout);
  14. if (fgets(buf, sizeof buf, stdin) == NULL)
  15. return EXIT_FAILURE;
  16.  
  17. printf("Enter a character to search for: ");
  18. fflush(stdout);
  19. if ((ch = getchar()) == EOF)
  20. return EXIT_FAILURE;
  21.  
  22. if (check(buf, ch))
  23. printf("%c was found\n", ch);
  24. else
  25. printf("%c was not found\n", ch);
  26.  
  27. return EXIT_SUCCESS;
  28. }
  29.  
  30. int check(char str[], int search)
  31. {
  32. return strchr(str, search) != NULL;
  33. }
You can also do it manually if you want.
  1. int check(char str[], int search)
  2. {
  3. int i;
  4.  
  5. for (i = 0; str[i] != '\0'; i++) {
  6. if (search == str[i])
  7. return 1;
  8. }
  9.  
  10. return 0;
  11. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 2
Reputation: cricket is an unknown quantity at this point 
Solved Threads: 0
cricket cricket is offline Offline
Newbie Poster

Re: Problem with string search of an array

 
0
  #3
Jan 30th, 2005
Thank you very much. I will try this.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,862
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Problem with string search of an array

 
0
  #4
Jan 31st, 2005
[QUOTE=Siersan]Using strchr it would look something like this.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int check(char str[], int search);
  6.  
  7. int main(void)
  8. {
  9. char buf[BUFSIZ];
  10. int ch;
  11.  
  12. printf("Enter a string: ");
  13. fflush(stdout);
  14. if (fgets(buf, sizeof buf, stdin) == NULL)
  15. return EXIT_FAILURE;
  16.  
  17. printf("Enter a character to search for: ");
  18. fflush(stdout);
  19. if ((ch = getchar()) == EOF)
  20. return EXIT_FAILURE;
  21.  
  22. if (check(buf, ch))
  23. printf("%c was found\n", ch);
  24. else
  25. printf("%c was not found\n", ch);
  26.  
  27. return EXIT_SUCCESS;
  28. }
  29.  
  30. int check(char str[], int search)
  31. {
  32. return strchr(str, search) != NULL;
  33. }
Just a friendly note:
fgets() will append a \n to the string, if you then just press enter at the character search prompt you will find the \n. The answer will be: was found
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem with string search of an array

 
0
  #5
Jan 31st, 2005
>if you then just press enter at the character search prompt you will find the \n.
That's reasonable (and expected) behavior. Let's say you want to test for exceptionally long lines:
  1. while (fgets(buffer, sizeof buffer, stdin) != NULL && !check(buffer, '\n')) {
  2. /* Append buffer to a dynamic string */
  3. }
If you don't want to find the newline, then remove it before the test, or verify the value of the search character as being neither EOF nor '\n'.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,862
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Problem with string search of an array

 
0
  #6
Jan 31st, 2005
Originally Posted by Narue
>if you then just press enter at the character search prompt you will find the \n.
That's reasonable (and expected) behavior.
No argument with that! It's the result telling me that an invisible character was found that can be confusing to the user.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC