| | |
Problem with string search of an array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2005
Posts: 2
Reputation:
Solved Threads: 0
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;
}
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;
}
Using strchr it would look something like this.
You can also do it manually if you want.
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> int check(char str[], int search); int main(void) { char buf[BUFSIZ]; int ch; printf("Enter a string: "); fflush(stdout); if (fgets(buf, sizeof buf, stdin) == NULL) return EXIT_FAILURE; printf("Enter a character to search for: "); fflush(stdout); if ((ch = getchar()) == EOF) return EXIT_FAILURE; if (check(buf, ch)) printf("%c was found\n", ch); else printf("%c was not found\n", ch); return EXIT_SUCCESS; } int check(char str[], int search) { return strchr(str, search) != NULL; }
C++ Syntax (Toggle Plain Text)
int check(char str[], int search) { int i; for (i = 0; str[i] != '\0'; i++) { if (search == str[i]) return 1; } return 0; }
[QUOTE=Siersan]Using strchr it would look something like this.
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
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> int check(char str[], int search); int main(void) { char buf[BUFSIZ]; int ch; printf("Enter a string: "); fflush(stdout); if (fgets(buf, sizeof buf, stdin) == NULL) return EXIT_FAILURE; printf("Enter a character to search for: "); fflush(stdout); if ((ch = getchar()) == EOF) return EXIT_FAILURE; if (check(buf, ch)) printf("%c was found\n", ch); else printf("%c was not found\n", ch); return EXIT_SUCCESS; } int check(char str[], int search) { return strchr(str, search) != NULL; }
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!
>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:
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'.
That's reasonable (and expected) behavior. Let's say you want to test for exceptionally long lines:
C++ Syntax (Toggle Plain Text)
while (fgets(buffer, sizeof buffer, stdin) != NULL && !check(buffer, '\n')) { /* Append buffer to a dynamic string */ }
I'm here to prove you wrong.
•
•
•
•
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.
May 'the Google' be with you!
![]() |
Similar Threads
- Search Array for word Pattern (C++)
- Binary Search Array From A File Help (C++)
- Problem with a string (char array) and self made strcpy (C++)
- Problem in String Search:Please Help (C)
Other Threads in the C++ Forum
- Previous Thread: Help with Error message
- Next Thread: help with namespace error
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






