My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

Two sentences are given and we were required to print the common word in the two sentences. Words were delimited by space, full stop, ? , ! and , . The trick in the question was that common word should be printed only once so if ur first sentence contains two word "how" and second contains "how" then in output how should be printed only once for this u can replace each matched word in second sentence by spaces .

Wednesday, September 23, 2009

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{    clrscr();
    char str1[500],str2[500];
    char substr1[50][50],substr2[50][50];
    for(int l=0;l<50;l++)
    {    strcpy(substr1[l],"");
        strcpy(substr2[l],"");
    }

    cout<<"enter the first string: ";
    gets(str1);
    cout<<"enter the second string: ";
    gets(str2);
    int i=0,index=0,s1index=0;

    while(str1[i]!='\0')
    {    if(str1[i]==' '||str1[i]=='?'||str1[i]=='.'||str1[i]=='!'||str1[i]=='_')
        {    for(int j=index, k=0;j<i;j++,k++)
            {    substr1[s1index][k]=str1[j];
            }

            substr1[s1index][k]='\0';
            s1index++;
            index=i+1;
        }
        i++;
    }
    index=0;
    s1index=0;
    i=0;
    while(str2[i]!='\0')
    {    if(str2[i]==' '||str2[i]=='?'||str2[i]=='.'||str2[i]=='!'||str2[i]=='_')
        {    for(int j=index, k=0;j<i;j++,k++)
            {    substr2[s1index][k]=str2[j];
            }

            substr2[s1index][k]='\0';
            s1index++;
            index=i+1;
           }
           i++;
    }
    cout<<"repeated words are: ";
    i=0;
     //    for(int g=0;g<5;g++)
     //    {cout<<substr1[g]<<endl;}
     //    for(int d=0;d<5;d++)
     //    {cout<<substr2[d]<<endl;}
    while(strcmp(substr1[i],"")!=0)
    {        int j=0;
         while(strcmp(substr2[j],"")!=0)
        {      if(strcmp(substr1[i],substr2[j])==0)
            cout<<substr1[i];
            j++;
        }
        i++;

    }
    getch();



}

0 comments: