# 15.4 – Operations on Strings Part 1

by on August 13, 2018

As the string is a character array, we need to develop logic to perform operations like finding the length, copying a string, reversing a string, extracting a part of string and concatenating a string with another string etc.

In this session we will see how different operations can be performed on strings

## Finding the length of a string

Logic:

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

Here the loop iterates as long as the end of character is not reached, terminates on reaching the end of character.

Using the variable “len” we are counting the number of times the loop iterates which is the length of string excluding with ‘\0’ because loop terminated on reaching end of character.

Program:

```#include<stdio.h>
int length(char*);
int main()
{
char x[50];
int l;
printf("Enter any string:");
scanf("%s",x);
l=length(x);                           /* sending address of string as argument */
printf("The length of string %d",l);
return 0;
}
int length(char *p)
{
int len,i;
for(len=0,i=0;p[i]!='\0';i++)   /* loop runs until the end of character reached */
len++;
return len;
}
```

Execution:
Enter any string: America
The length of string 7

### strlen()

In the above example we have developed our own logic and function to find the length of a string.
strlen() is a predefined function in the header file “string.h” used to find the length of string.
It accepts the string (address of array) as argument and returns the length excluding with ‘\0’.

Example:

```#include<stdio.h>
#include<string.h>
int main()
{
char x[50];
int l;
printf("Enter any string:");
scanf("%s",x);
l=strlen(x);
printf("The length of string %d",l);
return 0;
}
```

Execution:
Enter any string: America
The length of string 7

Example explained:
Here the predefined function strlen() is used to find the length

## Reversing a string

Logic:

In order to reverse the above string, p[0] and p[6] must be inter changed, p[1] and p[5] must be interchanged and must continue up to the middle of array.
Here, elements from first are selected using the index variable i, elements from last are selected using the index variable j.

```i=0;
```

Places the i at the beginning of array

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

As the loop is terminated with a semicolon, continue iterating as long as no terminating character is accesses and terminates on reaching the end of character ‘\0’. Now the j is on ‘\0’.

```j--;
```

It places the index pointer j before one place to string terminator
Now i is on the first element and j is on the element before to the terminator
Now to continue interchanging i should be incremented by 1 and j must be decremented by 1 for every iteration and p[i] and p[j] must be interchanged. It must continue as long as i<j .

```for(j=0;p[j]!='\0';j++);
i=0;
j--;
while(i<j)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
i++;
j--;
}
```

The same loop can be written using for loop as

```for(j=0;p[j]!='\0';j++);
for(i=0,j--;i<j;i++,j--)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
```

Program:

```#include<stdio.h>
void reverse(char*);
int main()
{
char x[50];
int i;
printf("Enter any string:");
scanf("%s",x);
reverse(x);
printf("The reverse string %s",x);
return 0;
}
void reverse(char *p)
{
int i,j;
char temp;
for(j=0;p[j]!='\0';j++); /* sends the j to end of string */
for(i=0,j--;i<j;i++,j--)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
```

Execution:
Enter any string: America
The reverse string aciremA

### strrev():

In the above example we have developed a function called reverse() to reverse a string.
strrev() is a predefined function defined within the header file “string.h” used to reverse a string.
It accepts the address of a string as an argument and reverses the string.

Note: gcc doesn’t provide strrev()

Example:

```#include<stdio.h>
#include<string.h>
int main()
{
char x[50];
int i;
printf("Enter any string:");
scanf("%s",x);
strrev(x);
printf("The reverse string %s",x);
printf("%s",x);
return 0;
}
```

Execution:
Enter any string: America
The reverse string aciemA

## Copying a string

Logic
Because string is a character array we can’t copy an array onto another array directly.
In order to copy a string to another, we need to copy character by character until the end of character reached and a terminating character ‘\0’ must be added at the end of target array to make it as a string.

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

The above loop copies character by character from source to destination and terminates when end of character ‘\0’ is reached.

The above loop does not store end of character in the destination. An end of character must be stored at the end of destination array to make it as a string

```q[i]='\0';
```

It explicitly stores the end of character in the destination array

Program:

```#include<stdio.h>
void copy(char*,char*);
int main()
{
char x[100],y[100];
printf("Enter any string:");
scanf("%s",x);
copy(y,x);
printf("%s",y);
return 0;
}
void copy(char *q,char *p)
{
int i;
for(i=0;p[i]!='\0';i++)
q[i]=p[i];
q[i]='\0';
}
```

Execution:
Enter any string: India
India

### strcpy():

In the above example we have developed our own function to copy a string on to another.
strcpy() is a predefined function defined with in the header file “string.h” used to copy a string onto another.
It accepts the addresses of both target and source strings as arguments and copy the contents of Source string on to target string.

Example:

```#include<stdio.h>
#include<string.h>
int main()
{
char x[50],y[50];
printf("Enter any string:");
scanf("%s",x);
strcpy(y,x);  /* "x" is copied onto "y" */
printf("%s",y);
return 0;
}
```

Execution:
Enter any string: India
India

Example explained:
The source string “x” is copied onto “y”

Previous post:

Next post: