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

shk14

shk14

@shk14-3VeyMQ Oct 11, 2024
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 <stdio.h>
#include<conio.h>
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • silverscorpion

    silverscorpion

    @silverscorpion-iJKtdQ Jun 27, 2012

    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

    @shk14-3VeyMQ Jun 27, 2012

    k..i did what u said...n nw the code is..
    #include <stdio.h>
    #include <stdlib.h>
    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.... 😔