0% found this document useful (0 votes)
37 views

String Operations

This document discusses various string handling functions in C including strlen(), strcmp(), strcat(), strcpy(), strncpy(), strrev(), strlwr(), and strupr(). It provides code examples to find the length of a string, compare two strings, concatenate strings, copy strings, reverse a string, convert a string to lowercase, and convert a string to uppercase. The examples demonstrate basic usage of these string functions and show their output when different strings are passed as arguments.

Uploaded by

23102120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

String Operations

This document discusses various string handling functions in C including strlen(), strcmp(), strcat(), strcpy(), strncpy(), strrev(), strlwr(), and strupr(). It provides code examples to find the length of a string, compare two strings, concatenate strings, copy strings, reverse a string, convert a string to lowercase, and convert a string to uppercase. The examples demonstrate basic usage of these string functions and show their output when different strings are passed as arguments.

Uploaded by

23102120
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

// 1.

Finding String length

//Using strlen() built-in function


#include<stdio.h>
#include<string.h>
void main()
{
char a[20];
int l;
printf("Enter the string\n");
gets(a);
l=strlen(a);
printf("The length of the string %s is %d",a,l);
}
Output:
Enter the string
Hello
The length of the string Hello is 5

// 2. String Comparison
//Using strcmp() built-in function
#include<stdio.h>
#include<string.h>
void main()
{
int d=0;
char a[20],b[20];
puts("Enter the string 1\n");
gets(a);
puts("Enter the string 2\n");
gets(b);
d=strcmp(a,b);
if(d==0)
printf("Both strings are equal");
else
printf("Both strings are not equal");
}
Output:
Enter the string 1
Welcome
Enter the string 2
Welcome
Both strings are equal

//Using strcmpi() built-in function


#include<stdio.h>
#include<string.h>
void main()
{
int d=0;
char a[20],b[20];
puts("Enter the string 1\n");
gets(a);
puts("Enter the string 2\n");
gets(b);
d=strcmpi(a,b);
if(d==0)
printf("Both strings are equal");
else
printf("Both strings are not equal");
}
Output:
Enter the string 1
Hello
Enter the string 2
Welcome
Both strings are not equal

//Using strncmp() built-in function


#include<stdio.h>
#include<string.h>
void main()
{
int d=0,n;
char a[20],b[20];
puts("enter the string 1\n");
gets(a);
puts("enter the string 2\n");
gets(b);
printf("enter the no. of characters to be compared “);
scanf("%d",&n);
d=strncmp(a,b,n);
if(d==0)
printf("string portions are equal ");
else
printf("string portions are not equal ");
}
Output:
enter the string 1
hello
enter the string 2
Hi
enter the no. of characters to be compared
1
string portions are equal

// 3. Concatenating two strings


//Using strcat() built-in function
#include<stdio.h>
#include<string.h>
void main()
{
char a[20],b[20];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
puts("The concatenated string is ");
puts(a);
}
Output:
Enter the first string
Hello
Enter the second string
World
The concatenated string is HelloWorld

//Using strncat() built-in function


#include<stdio.h>
#include<string.h>
void main()
{
char a[20],b[20];
int n;
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
puts("Enter no. of characters to be concatenated\n");
scanf("%d",&n);
strncat(a,b,n);
puts("The concatenated string is ");
puts(a);
}
Output:
Enter the first string
Hello
Enter the second string
World
Enter no. of characters to be concatenated
3
The concatenated string is HelloWor

// 4. String Copy
// Using strcpy() built-in function
#include<stdio.h>
#include<string.h>
void main()
{
char a[20],b[20];
printf("Enter the string to be copied\n");
gets(a);
strcpy(b,a);
puts("The string is copied");
puts(b);
}
Output:
Enter the string to be copied
Good
The string is copied

// Using strncpy() built-in function


#include<stdio.h>
#include<string.h>
void main()
{
char a[20], b[20];
int n;
printf("Enter the string to be copied\n");
gets(a);
printf("Enter the no. of characters to be copied\n");
scanf("%d",&n);
strncpy(b,a,n);
b[n]='\0';
puts("The string is copied");
puts(b);
}
Output:
Enter the string to be copied
Good
Enter the no. of characters to be copied
3
The string is copied

5. Using strrev() Function


#include<stdio.h>
#include<string.h>
void main()
{
char s[10];
printf(“Enter the string:”);
gets(s);
printf(“Reversed string = %s”,strrev(s));
}
Output:
Enter the string: Hello
Reversed string = olleH
6. Using strlwr() Function
#include<stdio.h>
#include<string.h>
void main()
{
char s[10];
printf(“Enter the string:”);
gets(s);
printf(“The lower case string = %s”,strlwr(s));
}
Output:
Enter the string: Hello
The lower case string = hello

7. Using strupr() Function


#include<stdio.h>
#include<string.h>
void main()
{
char s[10];
printf(“Enter the string:”);
gets(s);
printf(“The upper case string = %s”,strupr(s));
}
Output:
Enter the string: Hello
The upper case string = HELLO

You might also like