LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-06-2013, 03:30 PM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
c programming :searching a sub string in a string


I was studying this code and got a segmentation fault .Please help me with this ..
Code:
#include<stdio.h>
#include<string.h>
int search(char[],char[]);

int main()
{
char a[100],b[40];
int loc;

printf("n Enter the main string :");
gets(a);

printf("n Enter the search string :");
gets(b);

loc = search(a,b);

if(loc==-1)
      printf("nNot found");
else
      printf("nFound at location %d",loc+1);

return(0);
}

int search(char a[],char b[])
{
int i,j,firstOcc;
i=0,j=0;

while(a[i]!='')
  {
     while(a[i]!=b[0] && a[i]!='')
         i++;
     if(a[i]=='')
        return(-1);     //search can not continue

    firstOcc = i;

     while(a[i]==b[j] && a[i]!='' && b[j]!='')
         {
         i++;
         j++;
         }

    if(b[j]=='')
        return(firstOcc);
    if(a[i]=='')
        return(-1);

    i = firstOcc + 1;
    j = 0;
  }
}
 
Old 02-06-2013, 04:18 PM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 393

Rep: Reputation: 157Reputation: 157
Never use gets().

Quote:
while(a[i]!='')
This is not valid C. You probably meant while (a[i]). The same applies for the rest of the code.

Quote:
while(a[i]==b[j] && a[i]!='' && b[j]!='')
If a[i]==b[j] and a[i]!=0 (since that's what you meant), than there's no need to check b[j].

Compile your program with gcc -ggdb and than run it through gdb:
Code:
$ gcc -ggdb -o example example.c
$ gdb
(gdb) file example
(gdb) run
PS. Never use gets().
 
Old 02-06-2013, 05:26 PM   #3
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by mina86 View Post
This is not valid C. You probably meant while (a[i]). The same applies for the rest of the code.
I'm sure the OP meant '\0' instead of ''. In a true/false test a[i] is true/false the same as a[i]!='\0' so your suggestion of removing the !='' would work as well as the clearer alternative of add the \0

Since the code wouldn't even compile, the reported seg fault is hard to explain.

With correct input the program adjusted just enough to compile then works correctly and does not seg fault.

Rather than just tell someone never use gets() it is better to explain that when the input is longer than the buffer, gets() will corrupt memory with almost any result possible, including seg fault. Then the beginner writing the code can decide whether input longer than buffer is a condition he chooses to worry about.
 
  


Reply

Tags
programing


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
searching only particuler string? Shahid nx Linux - Newbie 5 04-09-2012 08:53 AM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
Searching a partition for a string Completely Clueless Linux - Newbie 40 08-03-2009 11:45 AM
searching for char string containing a '/' cleopard Programming 3 08-14-2008 04:17 PM
Searching for a string krazykow Solaris / OpenSolaris 1 03-17-2005 11:55 AM


All times are GMT -5. The time now is 09:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration