OPERATIONS ON STRING
1. Finding the Length of a String
Steps
1. Start with an index variable set to 0.
2. Check if the character at the current index is the null terminator character ('\0').
3. If the character is the null terminator character, return the index as the length of the string.
4. If the character is not the null terminator character, increment the index and repeat steps 2 and 3.
Algorithm
Program
#include <stdio.h>
int main()
{
char str[100];
int i, len;
printf("Enter a string: "); TIP
scanf("%s", str);
// initialize i to start at 0
i = 0; i=0
// iterate until null character is reached
while(str[i] != '\0') (str[0] != '\0')T (str[1] != '\0')T (str[2] != '\0')T (str[3] != '\0')T (str[4] != '\0')F
{
i++; i=1 i=2 i=3 i=4
}
len = i; len=4
printf("Length of string: %d", len); Length of string: 4
return 0;
}
String handling function: strlen(str)
2. Concatenating two strings to form a new string
Algorithm
Program
#include<stdio.h>
int main()
{
char str1[100],str2[100], str3[100];
int i=0,j=0; i=0,j=0
printf("\nEnter First String: "); TIP
gets(str1);
printf("\nEnter Second String: "); SAVE
gets(str2);
while(str1[i]!='\0') (str1[0]!='\0')T (str1[1]!='\0')T (str1[2]!='\0')T (str3[3]!='\0')F
{ T I P
str3[j]=str1[i]; str3[0]=str1[0] str3[1]=str1[1] str3[2]=str1[2]
i++; i=1 i=2 i=3
j++; j=1 j=2 j=3
}
i = 0; i=0
while(str2[i]!='\0') (str2[0]!='\0')T (str2[1]!='\0')T (str2[2]!='\0')T (str2[3]!='\0')T (str2[4]!='\0')F
{ S A V E
str3[j]=str2[i]; str3[0]=str2[0] str3[1]=str2[1] str3[2]=str2[2] str3[3]=str2[3]
i++; i=1 i=2 i=3 i=4
j++; j=1 j=2 j=3 j=4
}
str3[j]='\0'; str3[4]='\0'
printf("\nConcatenated String is: ”); Concatenated String is: TIPSAVE
puts(str1);
return 0;
}
String handling function: strcat(str1, str2)
3. Comparing two strings
Algorithm
Program
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i = 0; i=0
int len1=0, len2 = 0, same =0; len1=0, len2 = 0, same =0
printf("Enter the first string: "); Enter the first string:
gets(str1); TIP
printf("Enter the second string: "); Enter the second string:
gets(str2); SAVE
len1 = strlen(str1); len1=3
len2 = strlen(str2); len2=4
if(len1 == len2) (3 == 4)
{
while(i<len1)
{
if(str1[i] == str2[i])
i++;
else
break;
}
if(i == len1)
{
same = 1;
printf(“ The two strings are equal.”);
}
}
if(len1!= len2) (len1!= len2)
printf(“ The two strings are not equal.”); The two strings are not equal.
if(same == 0) (0 == 0)
{
if (str1[i] > str2[i]) (str1[0] > str2[0])
{
printf("String 1 is greater than String 2.\n"); String 1 is greater than String 2.
}
else if (str1[i] < str2[i])
{
printf("String 2 is greater than String 1.\n");
}
}
return 0;
String handling function: strcmp(str1, str2)
4. Reversing a string
Algorithm
Program
#include<stdio.h>
int main()
{
char str[100], reverse_str[100], temp;
int start = 0, end =0; start = 0, end =0
printf (" Enter a string to be reversed: "); Enter a string to be reversed:
gets(str); TIP
end = strlen (str1) - 1; end=3-1=2
while ( start < end) (0 < 2)T (1 < 1)F
{
temp = str [end]; temp=str[2]=P
str [end] = str[start]; str [end] =str[0]=T
str[start] = temp; str[start] =P
start++; start=1
end--; end=1
}
printf (" The reversed of the string:”); The reversed of the string: PIT
puts(str);
return 0;
}
5. Copy string
Program
#include <stdio.h>
int main()
{
char source[100], dest[100];
int i;
printf("\n Please Enter any String : "); Please Enter any String :
gets(source); source=CAN
for (i = 0; source[i]!='\0'; i++) i=0, (source[0]!='\0')T i=1, (source[1]!='\0')T i=2, (source[2]!='\0')T i=3, (source[3]!='\0')F
{
dest[i] = source[i]; dest[0] = source[0]=C dest[1] = source[1]=A dest[2] = source[2]=N
}
dest[i] = '\0'; dest[3] = '\0'
printf("\n String that we copied into dest = %s", dest); String that we copied into dest = CAN
printf("\n Total Number of Characters that we copied = %d\n", i); Total Number of Characters that we copied = 3
return 0;
}
String handling function: strcpy(dest, source)
Miscellaneous string and character functions
1. Character Manipulation Functions
2. String Manipulation Functions
#include <stdio.h>
#include <string.h>
int main()
{
Syntax of srtlen():
Find the length of a string excluding ‘\0’ NULL char str[] = "Hello World";
character. printf("The length of the string is: %zu\n", strlen(str));
size_t strlen(const char *str);
return 0;
}
OUTPUT
strlen() The length of the string is: 11
#include <stdio.h>
#include <string.h>
int main()
{
Syntax of srtcpy(): char src[] = "Hello World";
char dest[20];
Copies a string from the source to the destination. char *strcpy(char *dest, const strcpy(dest, src);
char *src); printf("The copied string is: %s\n", dest);
return 0;
}
Output:
strcpy() The copied string is: Hello World
#include <stdio.h>
#include <string.h>
int main() {
int n=3;
char str1[] = "Hello";
char str2[] = "World";
Copies n characters from source to the destination. strncpy( dest, src, n );
strncpy(str1, str2, n);
printf("The concatenated string is: %s\n", str1);
return 0;
}
Output:
strncpy() The concatenated string is: Worlo
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
Syntax of strcat():
char str2[] = "World";
Concatenate one string to the end of another. char *strcat(char *dest, const
strcat(str1, str2);
printf("The concatenated string is: %s\n", str1);
char *src);
return 0;
}
Output:
strcat() The concatenated string is: HelloWorld
#include <stdio.h>
#include <string.h>
int main() {
int n=3;
char str1[] = "Hello";
Concatenate n characters from the string pointed to strncat(dest, src, n); char str2[] = "World";
by src to the end of the string pointed to by dest. strncat(str1, str2, n);
printf("The concatenated string is: %s\n", str1);
return 0;
}
Output:
strncat() The concatenated string is: HelloWor
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if(result == 0) {
printf("The strings are equal.\n");
}
Syntax of strcmp():
else if(result < 0) {
Compares these two strings lexicographically. int strcmp(const char *str1,
printf("The first string is less than the second
string.\n");
const char *str2);
}
else {
printf("The first string is greater than the second
string.\n");
}
return 0;
}
Output:
strcmp() The first string is smaller than the second string.
#include <stdio.h>
#include <string.h>
Compares first n characters from the two strings
strncmp(s1, s2, n);
lexicographically. int main() {
strncmp() int n=3;
char str1[] = "Hello";
char str2[] = "Helld";
int result = strncmp(str1, str2, n);
if(result == 0) {
printf("The strings are equal.\n");
}
else if(result < 0) {
printf("The first string is less than the second
string.\n");
}
else {
printf("The first string is greater than the second
string.\n");
}
return 0;
}
Output:
The strings are equal.
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main()
{
char buffer[SIZE] = "computer program";
char * ptr;
int ch = 'p';
char *strchr(const char *str,
Find the first occurrence of a character in a string. int c) ptr = strchr( buffer, ch );
printf( "The first occurrence of %c in '%s' is '%s'\n",
ch, buffer, ptr );
return 0;
}
Output:
The first occurrence of p in 'computer program' is 'puter
program'
strchr()
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char buffer1[SIZE] = "computer program";
char * ptr;
char *strrchr(const char *str,
Find the last occurrence of a character in a string. int c)
int ch = 'p';
ptr = strrchr( buffer1, ch );
printf( "The first occurrence of %c in '%s' is '%s'\n",
ch, buffer1, ptr );
Output:
strrchr() program
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "hello world";
char str2[] = "world";
char *ptr;
Syntax of strstr():
First occurrence of a substring in another string. char *strstr(const char *str1,
ptr = strstr(str1, str2);
const char *str2);
printf("Substring found at: %s\n", ptr);
return 0;
}
Output:
strstr() world
#include <stdio.h>
int main()
int sprintf(char *str, const
Format a string and store it in a string buffer. char *string,...);
{
char buffer[50];
sprintf() int a = 10, b = 20, c;
c = a + b;
sprintf(buffer, "Sum of %d and %d is %d", a, b, c);
// The string "sum of 10 and 20 is 30" is stored
// into buffer instead of printing on stdout
printf("%s", buffer);
return 0;
}
Output:
Sum of 10 and 20 is 30
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Delhi,Hyderabad,Noida";
char *token;
/* get the first token */
token = strtok(str, ",");
/* loop through the string to extract all other tokens */
while(token != NULL) {
strtok(char * str, const char * printf("%s\n", token);
Split a string into tokens based on specified delimiters. delimiters); token = strtok(NULL, ",");
}
return 0;
}
Output:
Delhi
Hyderabad
strtok() Noida