I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
Index » Programming » C/C++ »

string handling problem

shamnar\′s Photo
27 Mar 06, 5:15AM
(1 reply)
string handling problem
:oops: hi i want a fn which accepts two strings as input and return another string as o/p the o/p should remove second string fron first string. for example if i/ps are "this" and "is" the o/p should be "th" pls help me if anyone can
nicholdug\′s Photo
30 Nov 06, 9:28AM
Dear Shamnar,

Here is your solution for string problem. I have solved it using Turbo 3.0. Pls. feel free if you need. Just copy the program using notepad using .txt extension and open it in Turbo IDE. Save the Program in Turbo IDE using .C extension. just press CTRL+F9. Input two strings and see the output. str1 is your answer.

[Your problem was: i want a fn which accepts two strings as input and return another string as o/p the o/p should remove second string fron first string. for example if i/ps are "this" and "is" the o/p should be "th" pls help me if anyone can.]

ANSWER:
void main()
{
        char str1[25];
	char str2[25];
	int flag=1;
	char *ptr1, *ptr2, *temp;
	clrscr();
 
	printf("Enter two string");
	gets(str1);
	gets(str2);
 
	ptr1=str1;
	ptr2=str2;
 
	while(*ptr1 != '\0')
	{
		while(*ptr2 != '\0')
		{
			if(*ptr1 != *ptr2)
				ptr2++;
			else
			{
				temp=ptr1;
				while(*ptr1 != '\0')
				{
					if(*ptr1 != *ptr2)
					{
						flag=0;
						break;
					}
					ptr1++;
					ptr2++;
				}
				if(flag==1)
					*temp='\0';
			}
		}
		ptr1++;
		ptr2=str2;
	}
                printf("\nThe output is\t");
	puts(str1);      // In str1 the repeated string has been removed.
 
	getch();
}
Currently you need to be logged in to leave a message.