943,936 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9008
  • C++ RSS
Jan 30th, 2005
0

Problem with string search of an array

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cricket is offline Offline
2 posts
since Jan 2005
Jan 30th, 2005
0

Re: Problem with string search of an array

Using strchr it would look something like this.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 12
Solved Threads: 2
Light Poster
Siersan is offline Offline
45 posts
since Jan 2005
Jan 30th, 2005
0

Re: Problem with string search of an array

Thank you very much. I will try this.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cricket is offline Offline
2 posts
since Jan 2005
Jan 31st, 2005
0

Re: Problem with string search of an array

[QUOTE=Siersan]Using strchr it would look something like this.
C++ Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 31st, 2005
0

Re: Problem with string search of an array

>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:
C++ Syntax (Toggle Plain Text)
  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'.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 31st, 2005
0

Re: Problem with string search of an array

Quote 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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with Error message
Next Thread in C++ Forum Timeline: help with namespace error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC