![]() |
| ||
| Problem with string search of an array 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; } |
| ||
| Re: Problem with string search of an array Using strchr it would look something like this. #include <stdio.h>You can also do it manually if you want. int check(char str[], int search) |
| ||
| Re: Problem with string search of an array Thank you very much. I will try this. |
| ||
| Re: Problem with string search of an array [QUOTE=Siersan]Using strchr it would look something like this. #include <stdio.h>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 |
| ||
| 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: while (fgets(buffer, sizeof buffer, stdin) != NULL && !check(buffer, '\n')) {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'. |
| ||
| Re: Problem with string search of an array Quote:
|
| All times are GMT -4. The time now is 1:50 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC