Searching string not working
-
I have a program to search and find a given string as below : #include #include #define MAX 10 int srch(char *a[],char *b) { int i,r=0; for(i=0;a[i];i++){ printf("%c\n",a[i]); if(!strcmp(a[i],b)){ return 0; } else{ return -1; } } } main() { char *a[]={"abc","xyz","lmn","NULL"}; char *b; int l; printf("\nenter string to find\n"); fgets(b,MAX,stdin); printf("%c",*b); l=srch(a,b); printf("\n%d",l); } But I am getting segmentation fault. Why?
-
I have a program to search and find a given string as below : #include #include #define MAX 10 int srch(char *a[],char *b) { int i,r=0; for(i=0;a[i];i++){ printf("%c\n",a[i]); if(!strcmp(a[i],b)){ return 0; } else{ return -1; } } } main() { char *a[]={"abc","xyz","lmn","NULL"}; char *b; int l; printf("\nenter string to find\n"); fgets(b,MAX,stdin); printf("%c",*b); l=srch(a,b); printf("\n%d",l); } But I am getting segmentation fault. Why?
Anonygeeker wrote:
But I am getting segmentation fault. Why?
Because your original array does not contain a null terminator. You have:
char *a[]={"abc","xyz","lmn","NULL"};
but
"NULL"
is a quoted string, not aNULL
value, so your loop will continue until it causes the fault. You also have:printf("%c\n",a[i]);
but the array is an array of pointers, not characters. You need to use the string identifier in your format string like:
printf("%s\n",a[i]); // use s to print a character aray
And finally (although I may have missed other mistakes) you have:
char *b; // what does this point to?
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin); // but b does not point to anything
printf("%c",*b); // again %c means a single characterIt should be:
char b[MAX];
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin);
printf("%s",b);I strongly suggest you get a good book on C and learn the basics of single elements, arrays, pointers, printf format strings etc.
-
Anonygeeker wrote:
But I am getting segmentation fault. Why?
Because your original array does not contain a null terminator. You have:
char *a[]={"abc","xyz","lmn","NULL"};
but
"NULL"
is a quoted string, not aNULL
value, so your loop will continue until it causes the fault. You also have:printf("%c\n",a[i]);
but the array is an array of pointers, not characters. You need to use the string identifier in your format string like:
printf("%s\n",a[i]); // use s to print a character aray
And finally (although I may have missed other mistakes) you have:
char *b; // what does this point to?
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin); // but b does not point to anything
printf("%c",*b); // again %c means a single characterIt should be:
char b[MAX];
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin);
printf("%s",b);I strongly suggest you get a good book on C and learn the basics of single elements, arrays, pointers, printf format strings etc.
Richard MacCutchan wrote:
although I may have missed other mistakes
You have missed the logical error here (which should generate a compiler message that not all paths return a value):
int srch(char *a[],char *b)
{
int i,r=0;
for(i=0;a[i];i++){
printf("%c\n",a[i]);
if(!strcmp(a[i],b)){
return 0;
}
else{
return -1;
}
}
}The loop will - if entered - always left after the first iteration. But it is hard to spot whithout indentation.
-
Anonygeeker wrote:
But I am getting segmentation fault. Why?
Because your original array does not contain a null terminator. You have:
char *a[]={"abc","xyz","lmn","NULL"};
but
"NULL"
is a quoted string, not aNULL
value, so your loop will continue until it causes the fault. You also have:printf("%c\n",a[i]);
but the array is an array of pointers, not characters. You need to use the string identifier in your format string like:
printf("%s\n",a[i]); // use s to print a character aray
And finally (although I may have missed other mistakes) you have:
char *b; // what does this point to?
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin); // but b does not point to anything
printf("%c",*b); // again %c means a single characterIt should be:
char b[MAX];
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin);
printf("%s",b);I strongly suggest you get a good book on C and learn the basics of single elements, arrays, pointers, printf format strings etc.
thankyou
-
Anonygeeker wrote:
But I am getting segmentation fault. Why?
Because your original array does not contain a null terminator. You have:
char *a[]={"abc","xyz","lmn","NULL"};
but
"NULL"
is a quoted string, not aNULL
value, so your loop will continue until it causes the fault. You also have:printf("%c\n",a[i]);
but the array is an array of pointers, not characters. You need to use the string identifier in your format string like:
printf("%s\n",a[i]); // use s to print a character aray
And finally (although I may have missed other mistakes) you have:
char *b; // what does this point to?
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin); // but b does not point to anything
printf("%c",*b); // again %c means a single characterIt should be:
char b[MAX];
int l;
printf("\nenter string to find\n");
fgets(b,MAX,stdin);
printf("%s",b);I strongly suggest you get a good book on C and learn the basics of single elements, arrays, pointers, printf format strings etc.
Try
#include #include #define MAX 10
int srch(const char *a[],char *b)
{
int i = 0;
while ( a[i] != NULL)
{
if ( ! strcmp( b, a[i]) ) return i;
++i;
}
return -1;
}int main()
{
int index;
const char *a[]={"abc","xyz","lmn", NULL};
char b[MAX];
printf("\nplease, enter string to find\n");if ( fgets(b, MAX, stdin) )
{
// remove the newline
for (index = 0; index < MAX; ++index)
if ( b[index] == '\n')
{
b[index] = '\0';
break;
}printf("string to find '%s'\\n",b); index = srch( a, b); if ( index != -1 ) printf("string found at index = %d\\n", index); else printf("string not found\\n");
}
return 0;
}Please note,
fgets
includes the newline in the string, that's the reason for the clumsy code used to remove it. -
I have a program to search and find a given string as below : #include #include #define MAX 10 int srch(char *a[],char *b) { int i,r=0; for(i=0;a[i];i++){ printf("%c\n",a[i]); if(!strcmp(a[i],b)){ return 0; } else{ return -1; } } } main() { char *a[]={"abc","xyz","lmn","NULL"}; char *b; int l; printf("\nenter string to find\n"); fgets(b,MAX,stdin); printf("%c",*b); l=srch(a,b); printf("\n%d",l); } But I am getting segmentation fault. Why?
Try
#include #include #define MAX 10
int srch(const char *a[],char *b)
{
int i = 0;
while ( a[i] != NULL)
{
if ( ! strcmp( b, a[i]) ) return i;
++i;
}
return -1;
}int main()
{
int index;
const char *a[]={"abc","xyz","lmn", NULL};
char b[MAX];
printf("\nplease, enter string to find\n");if ( fgets(b, MAX, stdin) )
{
// remove the newline
for (index = 0; index < MAX; ++index)
if ( b[index] == '\n')
{
b[index] = '\0';
break;
}printf("string to find '%s'\\n",b); index = srch( a, b); if ( index != -1 ) printf("string found at index = %d\\n", index); else printf("string not found\\n");
}
return 0;
}Please note,
fgets
includes the newline in the string, that's the reason for the clumsy code used to remove it. -
Richard MacCutchan wrote:
although I may have missed other mistakes
You have missed the logical error here (which should generate a compiler message that not all paths return a value):
int srch(char *a[],char *b)
{
int i,r=0;
for(i=0;a[i];i++){
printf("%c\n",a[i]);
if(!strcmp(a[i],b)){
return 0;
}
else{
return -1;
}
}
}The loop will - if entered - always left after the first iteration. But it is hard to spot whithout indentation.