Below is C program to Locate a character in String - STRCHR.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /* Locate a character in String - STRCHR */ #include<stdio.h> int main() { char str[500]; int i; char ch; printf ( "Please enter your string: " ); // gets(str); // fgets is a better option over gets to read multiword string . fgets (str, 500, stdin); // Following can be added for extra precaution for '\n' character // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL; printf ( "Please enter a character: " ); getchar (ch); for (i=0;str[i]!=NULL;i++) { if (str[i]==ch) break ; } if (str[i] == NULL) printf ( "%c not found in %s" ,ch,str); else printf ( "\"%c\" found in \"%s\" at %d location" ,ch,str,i+1); return 0; } |
No comments:
Post a Comment