C string Problem!!

10 replies [Last post]
programmer
Offline
Last seen: 10 years 16 weeks ago
Joined: 2009-04-14
bOt Points: 86

I have three strings:
 

C Syntax
 
  1. char str1[] = "My name is programmer";
  2. char str2[] = "My name is programmer also";
  3. char final [10];

How would i go about comparing the first two strings, and put what is different (" also") into the third one?

nataraja
Offline
Last seen: 6 years 1 week ago
Joined: 2009-06-11
bOt Points: 94
hi

get the string lengths of both and insert null charecters into short string

then compare using for loop .....

and record if their is a mismatch between char

ravinder singh
Offline
Last seen: 10 years 18 weeks ago
Joined: 2009-06-11
bOt Points: 124

 

  1. char str1[] = "My name is programmer";
  2. char str2[] = "My name is programmer also";
  3. char final [10];
  4.  
  5. if(strcmp(str1, str2) > 0)
  6. strcpy(final, &str1[strlen(str2)]);
  1. else
  2.   strcpy(final, &str2[strlen(str1)]);
  3.  
  4.  
programmer
Offline
Last seen: 10 years 16 weeks ago
Joined: 2009-04-14
bOt Points: 86
Query!!

@Ravinder

Wont your code work only for the case when the extra portion in string2 is located at the end?

 

wat if their is mismatch somewhere in between the string??

 

Can somebody help me with the code??

cardcaptor sakura
Offline
Last seen: 10 years 16 weeks ago
Joined: 2009-06-23
bOt Points: 66
in a long but fool proof

in a long but fool proof way

save each element in an array

for both arrays

check the characters 

Guest (not verified)
feasible solution

#include
#include

int main()

{

char str1[] = "My name is programmer";

char str2[] = "My name is programmer also";

char final [10];
int i=0,k=0;
printf("First : %s",str1);
printf("\nSecond : %s",str2);
while(str1[i]==str2[i])i++;
if(str1[i]=='\0')
do{final[k++]=str2[i];

}while(str2[i++]!='\0');
else
do{final[k++]=str1[i];

}while(str1[i++]!='\0');

printf("\nFinal : %s",final);
getchar();
return 0;

}

Guest (not verified)
feasible solution : edit

include stdio.h as header

Guest (not verified)
#include #include #include #i

#include
#include
#include
#include

// checks char differences
//any differences are stored in final as char in arr1
//followed by char in arr2

void main()
{
clrscr();
char arr1[20],arr2[20],final[40];
short int i=0,k=0,str1=0,str2=0,endcheck=0;
cout<<"\nEnter the first string :";gets(arr1);
cout<<"\nEnter the second string :";gets(arr2);
while(!endcheck)
{
if(arr1[i]!=arr2[i])
{
final[k++]=arr1[i];
final[k++]=arr2[i];
}
if(i==strlen(arr1)-1)
{
endcheck=1;
str1=1;
}
if(i==strlen(arr2)-1)
{
endcheck=1;
str2=1;
}
i++;
}
if(str1&&!str2)
for(i;i {
final[k++]=' ';
final[k++]=arr2[i];
}
if(!str1&&str2)
for(i;i {
final[k++]=arr1[i];
final[k++]=' ';
}
if(k)
{
cout<<"\nThe characters which vary are : ";
cout<<"\nString 1: ";
for(int i=0;i cout< cout<<"\nString 2: ";
for(i=1;i cout< getch();
}
else
{
cout<<"\nThe strings are the same.";
getch();
}
}

Guest (not verified)
for(int i=0;i

for(int i=0;i cout< cout<<"\nString 2: ";
for(i=1;i cout< getch();
}
else
{
cout<<"\nThe strings are the same.";
getch();
}
}

shashank (not verified)
another possible soln. can

another possible soln. can be:

step 1: take smaller of two lengths in 'n' through conditional statement

step 2:call strncmp(s1,s2,n)

step 3:if true,print(or return) the longer one.

..in fact step 2&3 can also be combined in a printf through conditional..i hope dats simpler..

j.sushanthkumar
Offline
Last seen: 10 years 7 weeks ago
Joined: 2009-08-21
bOt Points: 66
string
for(i<strlen(str2))

{

if(Str1[i]!=str2[i])

{

concatinate str2[i] to final

}

}

 

Note:- This post has been edited by j.sushanthkumar at Fri, 2009-08-21 18:20.