# 15.5 – Operations on Strings Part-2

by on August 14, 2018

## Concatenating a string to another string

Logic

Appending a string to another string is called concatenation. To concatenate a string to another string, we first send the index variable “i” to the end of first string using a loop

```for(i=0;i<p[i]!='\0';i++);
```

As the loop is terminated with a semicolon, loop continuous as long as the end of string character ‘\0’ is not reached and terminates on reaching the end of character ‘\0’. Now “i” is on ‘\0’
Now we copy the characters of second string onto first string using the loop

```for(j=0;q[j]!='\0';i++,j++)
p[i]=q[j];
```

Loop terminates when the end of string character is reached in the second string. A terminating character ‘\0’ is added at the end of first string to make it as a string.

Program:

```#include
void concat(char*,char*);
int main()
{
char x[50],y[50];
printf("Enter the 1st string:");
scanf("%s",x);
printf("Enter the 2nd string:");
scanf("%s",y);
concat(x,y);
printf("The resultant string is %s",x);
return 0;
}
void concat(char *p,char *q)
{
int i,j;
for(j=0;p[j]!='\0';j++);   /* setting "j" to the end of string */
for(i=0;q[i]!='\0';i++,j++) /* copying from source to target */
p[j]=q[i];
p[j]='\0';  /* adding terminating character */
}
```

Execution:
Enter the 1st string: hello
Enter the 2nd string: sir

The resultant string is hellosir

### strcat():

In the above example we have developed a function called concat() to concatenate a string to another.
strcat() is a predefined function defined within the header file “string.h” used to concatenate a string to another.
It accepts the addresses of target and source strings as arguments and concatenates the source string to the target string.

Example:

```#include
#include
int main()
{
char x[50],y[50];
printf("Enter the 1st string:");
scanf("%s",x);
printf("Enter the 2nd string:");
scanf("%s",y);
strcat(x,y);
printf("The resultant string is %s",x);
return 0;
}
```

Execution:
Enter the 1st string: hello
Enter the 2nd string: sir
The resultant string is hellosir

Example explained:
strcat() concatenates the source string (y) to target string (x)

## Comparing two strings Lexicographically

Logic to compare two strings:

• The function must return the ASCII difference among the first occurrence of in-equal characters.
• If it returns the +ve value then the first string is the biggest one.
• If it returns the –ve value then the second string is the biggest one.
• If it returns 0 then both the strings are equal.

Checked for in-equality “if(p[i]!=q[i])” character by character from the first character in both the strings, if find then ASCII difference is returned  ‘”return p[i]-q[i];”
If both the strings reach to the end without in-equality then 0 is returned.

```int compare(char *p,char *q)
{
int i;
for(i=0; (p[i]!='\0' || q[i]!='\0');i++)
if(p[i]!=q[i])
return p[i]-q[i];
return 0;
}

```

Different cases:

Program:

```#include
int compare(char*,char*);
int main()
{
char x[50],y[50];
printf("Enter the 1st string:");
scanf("%s",x);
printf("Enter the 2nd string:");
scanf("%s",y);
if(compare(x,y)==0)
printf("Equal");
else if(compare(x,y)>0)
printf("Biggest string is %s",x);
else
printf("Biggest string is %s",y);
return 0;
}
int compare(char *p,char *q)
{
int i;
for(i=0;(p[i]!='\0'||q[i]!='\0');i++)
if(p[i]!=q[i])
return p[i]-q[i];
return 0;
}

```

Execution1:
Enter the 1st string: niit
Enter the 2nd string: nice
Biggest string is niit

Execution2:
Enter the 1st string: nice
Enter the 2nd string: niit
Biggest string is niit

Execution3:
Enter the 1st string: smith
Enter the 2nd string: black
Biggest string is smith

Execution4:
Enter the 1st string: niit
Enter the 2nd string: niit
Equal

Execution5:
Enter the 1st string: run
Enter the 2nd string: runner
Biggest string is runner

### strcmp():

In the above example we have developed our own function to compare any two strings.
strcmp() is a predefined function defined within the header file “string.h”
It accepts two strings as arguments and returns the ASCII difference among the first occurrence of unequal characters.
It returns zero if both the strings are equal.

Example:

```#include
#include
int main()
{
char x[50],y[50];
printf("Enter the first string:");
scanf("%s",x);
printf("Enter the second string:");
scanf("%s",y);
if(strcmp(x,y)==0)
printf("Equal");
else if(strcmp(x,y)>0)
printf("Biggest string %s",x);
else
printf("Biggest string %s",y);
return 0;
}

```

Execution:
Enter the first string: hello
Enter the second string: world
Biggest string world
Example explained:
Here we have used predefined function to compare two strings.

{ 0 comments… add one now }

Previous post:

Next post: