Unable to display the strstr result if the sub-string is at the end of the string....
I'm stuck again, why am I unable to display the last character of the sub-string when the sub-string is located at the end of the string? I thought a NULL character is automatically appended to the array input ?
Also it seems that the search result won't capture the puntuation in the search. A search of sub-string "Hello!" in the string "Hello! World!" will only return 'Hell' (What an irony!!) :cool:
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void searchstr (char str[]);
main()
{
char str[100];
printf ("Enter the String :");
fgets (str, sizeof str, stdin);
searchstr (str);
return 0;
}
void searchstr (char str[])
{
char substr[100];
printf ("Enter the string you want to find: ");
fgets (substr, sizeof str, stdin);
if (strstr(str, substr))
printf ("the substring '%s' is found !!!", substr);
}
Is there anyway to achieve your guys level... any tips...:D
And also curious to find out the market protienal for C programming... Is it still in demand in the market ?
How about the punchuation ??? It will eat the words away!!!
:( But but.... A search of sub-string "Hello!" (includind the !) in the string "Hello! World!" will only return 'Hell' and the Hello is at the begning of the string, or is it the strstr function can only search for character and not the puntuation?? But, then again... if the punchutation is in the middle of the sub-string, it will still able to display without eating up the word..... I tried before....
still got problem.. help....
Why is it when I run the sub function programe is prefrectly fine but when I impliment it to the main programe it simply skip the input part... the fgets command....
here is the programe:
Code:
main()
{
char str[100];
int ch;
printf ("Enter the String :");
fgets (str, sizeof str, stdin);
printf ("\n");
printf ("1/ Count Number of Characters In String.\n");
printf ("2/ Count Number of Words In String.\n");
.......
void looksub (char str[])
{
char substr[100];
char *p;
printf ("Enter the substring you want to find: ");
fgets (substr, sizeof substr, stdin);
p = strchr(substr, '\n');
if(p) *p = '\0';
if (strstr(str, substr))
printf ("The substring '%s' is found !!!", substr);
else
printf ("The substring '%s' is not found!!!", substr);
}
When come to the looksub function, it straight away go to the printf statement, without allowing me to key in my option.. Why !!!
Here is an attachment of my file....
Genius !!!! Really thanks a lot...
Really thanks for your help... my life saviour... :) *kiss*
But how come with that extra line it make so much different ???
Code:
while(getchar() != '\n');
and when I run the programe, the one previosly on the forum, I can run it without that line...