Immanuel Training Institute proudly presents free software courses
To get certificate Contact: iits.certificate@gmail.com
CHAPTER 1 Introduction and structure of C program
CHAPTER 2 Data types, Constants, Variables and I/O Statements
CHAPTER 3 Operators and Expressions
CHAPTER 6 String Manipulations
CHAPTER 8 Structure, Unions and Enumerated Data types
CHAPTER
6
STRING MANIPULATION:-
String means the group of character, digit, and symbols enclosed within the quotation marks.
Ex:- char name[]={‘w’,’s’,’g’,’,’r’}
Initialize string:- char name[]=”ram”;
STRING STANDARD FUNCTION:-
· strlen(string_name) – To find the length of a given string
· strcpy(destination, source) – To copy source string into destination
· strcat(first_string, second_string) – To combine first & second string
· strcmp(first_string, second_string) – To compare first and second string
How it works?
Subtracting combined ASCII value of second string from first string,
If the result is >0 first string is big
If the result is <0 second string is big
If the result is =0 both are equal
· strrev(string) – It reverses the string characters
· strlwr(string) – It changes the string case to lowercase
· strupr(string) – It changes the string case to uppercase
EXAMPLE PROGRAMS:
1. Example
programs for string manipulation functions.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10],c[10];
clrscr();
printf("Enter the string1");
scanf("%s",a);
printf("Enter the string2:");
scanf("%s",b);
clrscr();
printf("\n\nThe length of the string1 is
%d",strlen(a));
printf("\n\nThe length of the string2 is
%d",strlen(b));
printf("\n\nThe combination of string1 &
string2 is %s",strcat(a,b));
printf("\n\nThe upper case of the string is
%s",strupr(a));
printf("\n\nThe copied string is
%s",strcpy(c,a));
printf("\n\nThe lower case of the copied string
is %s",strlwr(c));
printf("\n\nThe reverse order of the given
string is %s",strrev(c));
getch();
}
2. Comparing
two strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[10],b[10];
clrscr();
printf("Enter the string1:");
scanf("%s",a);
printf("Enter the string2:");
scanf("%s",b);
if((strcmp(a,b))==0)
printf("The
two strings are equal");
else
{
if((strcmp(a,b))>0)
printf("The
string1 is big");
else
printf("The
string2 is big");
}
getch();
}
3. Write
a program to check the given string is palindrome or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],t[10];
clrscr();
printf("Enter the string:");
scanf("%s",a);
printf("\nThe given string is %s",a);
strcpy(t,a);
printf("\n\nThe reverse order of the given
string is %s",strrev(t));
if(strcmp(a,t)==0)
printf("\n\nThe
given string is a palindrome word");
else
printf("\n\nThe
given string is not a palindrome word");
getch();
}
4. Write a program to arrange the given strings into
the alphabetical order.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10][10],i,n,j,t[20];
clrscr();
printf("Enter the no. of strings:");
scanf("%d",&n);
printf("Enter the strings:");
for(i=1;i<=n;i++)
scanf("%s",a[i]);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(t,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],t);
}
}
}
printf("The alphabetical order is:\n");
for(i=1;i<=n;i++)
printf("%s\n",a[i]);
getch();
}