location of a substring in a string

This is a discussion on location of a substring in a string within the C Programming forums, part of the General Programming Boards category; Hello,i have this program to search a sub string in string and tells its location but it is only telling ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    Exclamation location of a substring in a string

    Hello,i have this program to search a sub string in string and tells its location but it is only telling the position of first matched character.help me if u can.


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int searchsubstring(char *,char *);
    int position (char *,char*);
    
    
    int main()
    {
    char s1[10],s2[10];
    int i,v,j,f=0;
    printf("Enter main string : ");
    gets(s1);
    printf("Enter substring to be searched in main string : ");
    gets(s2);
    if(searchsubstring(s1,s2)!=0)
    printf("\nFound");
    else
    printf("\nNot found");
    v=position(s1,s2);
    
    printf ("\nLocation of substring: %d",v);
    }
    
    int searchsubstring(char *s1,char *s2)
    {
    int f=0;
    for(;*s1!='\0';)
    {
    if(*s2=='\0')
    break;
    for(;*s2!='\0';)
    {
    if(*s1==*s2)
    {
    f=1;
    s1++;
    s2++;
    }
    else
    {
    f=0;
    s1++;
    break;
    }
    }
    }
    if(f==0)
    return 0;
    else
    return 1;
    getch();
    }
    
    int position(char *s1,char *s2)
    {
    int f=0,x=0;
    for(;*s1!='\0';)
    {
    if(*s2=='\0')
    break;
    for(;*s2!='\0';)
    {
    if(*s1==*s2)
    
    {
    f=1;
    s1++;
    s2++;
    x++;
    return x;
    }
    else
    {
    f=0;
    s1++;
    x++;
    break;
    }
    }
    }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,910
    Please indent your code properly. Also, it will help if you use descriptive variable names: consider that s1 and s2 give no indication as to which is the string to be searched and which is the string that is searched for.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    64

    Exclamation

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int searchsubstring(char *,char *);
    int position (char *,char*);
    
    
    int main()
    {
    char s1[10],s2[10];
    int i,v,j,f=0;
    printf("Enter main string : ");
    gets(s1);// s1 main string
    printf("Enter substring to be searched in main string : ");
    gets(s2);//s2 sub string
    if(searchsubstring(s1,s2)!=0)//calling search function
    printf("\nFound");
    else
    printf("\nNot found");
    v=position(s1,s2);//calling position
    
    printf ("\nLocation of substring: %d",v);
    }
    //body of search function starts here
    int searchsubstring(char *s1,char *s2)
    {
    int f=0;
    for(;*s1!='\0';)
    {
    if(*s2=='\0')
    break;
    for(;*s2!='\0';)
    {
    if(*s1==*s2)
    {
    f=1;
    s1++;
    s2++;
    }
    else
    {
    f=0;
    s1++;
    break;
    }
    }
    }
    if(f==0)
    return 0;
    else
    return 1;
    getch();
    }
    //ends here
    
    //body of position function starts
    int position(char *s1,char *s2)
    {
    int f=0,x=0;
    for(;*s1!='\0';)
    {
    if(*s2=='\0')
    break;
    for(;*s2!='\0';)
    {
    if(*s1==*s2)
    
    {
    f=1;
    s1++;
    s2++;
    x++;
    return x;
    }
    else
    {
    f=0;
    s1++;
    x++;
    break;
    }
    }
    }
    }
    // program ends here
    Last edited by danishzaidi; 12-21-2011 at 11:06 AM. Reason: awain

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You STILL need to indent your code - that's a mess to have to study.

    You WANT to search "on your own", instead of using strstr() which is made for this?

    In your search loop, you need to move the position of the pointer (file pointer, or index), to the next char, and continue looping until you reach the end of the string (or get close enough that the substring you're searching for, is larger than the remaining letters in the string you're looking through).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing a substring from a string
    By aysek8 in forum C Programming
    Replies: 7
    Last Post: 11-05-2011, 01:22 PM
  2. Substring of a string
    By LotusE in forum C Programming
    Replies: 2
    Last Post: 01-23-2006, 02:36 PM
  3. substring in string
    By kenni81 in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 05:05 PM
  4. breaking string into substring
    By sadat in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 05:03 AM
  5. seeing if there is a substring in another string
    By canine in forum Windows Programming
    Replies: 2
    Last Post: 12-05-2001, 03:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21