c code problem in strings....pls help..!!!!

i created a c program taking two strings and comparing them character by character....the character from string 1 is ommited if it is presentin string 2....and finally prints the remaining string 1....following is the code.... bt its taking too long to execute....and m not able to get the solutn....pls help..
#include 
#include
void str(char *a,char *b);
char z[15];
int main() {
char a[15];
char b[15];
printf("enter string 1: ");
scanf("%s",a);
printf("enter string 2: ");
scanf("%s",b);
str(a,b); printf("%s",z);
}
void str(char *x,char *y) {
int i=0,leny=0,count=0;
char *p;
p=y;
while(*y!='\0')
{
leny++; y++;
}
while(*x!='\0')
{
y=p;
while(*y!='\0')
{
if(*x!=*y)
{
y++; count++;
}
else
{
break;
}
if(count==(leny+1))
{
z[I]=*x;[/I]
[I] i++; [/I]
[I]break;[/I]
[I] }[/I]
[I] }[/I]
[I] x++;[/I]
[I] }[/I]
[I] }
[/I]

Replies

  • silverscorpion
    silverscorpion
    You are modifying variable 'z' inside the function str(). That is a local variable which is visible only inside that function. Whatever changes you make to that variable will not be reflected in the variable 'z' used in main. If you want to store the remaining characters in another variable, you should pass that variable also as a parameter to the function. ie., declare z as a character pointer in main, and pass z as an additional parameter to str()
  • shk14
    shk14
    k..i did what u said...n nw the code is..
    #include
    #include
    void str(char *a,char *b,char *c);

    int main()
    {
    char a[15];
    char b[15];
    char z[15];
    printf("enter string 1: ");
    scanf("%s",a);
    printf("enter string 2: ");
    scanf("%s",b);
    str(a,b,z);
    printf("%s",z);

    }
    void str(char *x,char *y,char *z)
    {
    int i=0,leny=0,count;
    char *p;
    p=y;
    while(*y!='\0')
    {
    leny++;
    y++;

    }
    while(*x!='\0')
    {

    y=p;
    count=0;

    while(*y!='\0')
    {
    if(*x!=*y)
    {
    y++;
    count++;
    }
    else
    {

    break;

    }
    if(count==(leny))
    {
    *z=*x;
    z++;
    break;
    }
    }
    x++;

    }
    }



    but still its not giving the correct output.... 😔

You are reading an archived discussion.

Related Posts

Dear all, I'm Skylight 25 years old( real name Wangpo),a civil engineering graduate from a little country, Bhutan. I've graduated in the year 2010 June and have been working here...
Can sound energy be converted and stored in any form of energy ?
[I know no one likes to read long articles because they are boring. I however want you to read this article, think about it and if possible react to it...
Ace Engineering academy is conducting online tests for GATE and IES aspirants. The tests will be all online and can be availed with only a INR 1000 investment. Follow the...