Assignment No. 5 Strings.
Objectives: To study the string. The string is collection
of characters which end with null (‘\0’) character.
1. Write
a program to accept the string and count the length of string without using
standard library function.
Student
Name:Tambe
Sanjay Baban Roll
No:153
Program Name:Write
a program to accept the string and count the length of string without using standard library function.
#include<stdio.h>
#include<conio.h>
void
main()
{
char
str[53];
int
i;
clrscr();
printf("\n
enter the string-");
gets(str);
i=0;
while(str[i]!='\0')
{
i++;
}
printf("\n
the length of string is- %d",i);
getch();
}
/*
Output
Enter the string-Maharashtra
The length of string is- 11 */
2) Write
a program to copy one string into another string
Student
Name:Tambe Sanjay Baban Roll No:153
Program
Name: Write a program
to copy one string into another string
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[35];
clrscr();
printf("\n Enter the first string-");
gets(str1);
printf("\n Enter the second
string-");
gets(str2);
strcpy(str1,str2);
printf("\n
The string after coping");
puts(str1);
getch();
}
/*
Output
Enter the first string-India
Enter the second string-Maharashtra
The string after coping Maharashtra */
3)Write a program to accept the string and display
it reverse
Student
Name:Tambe
Sanjay Baban Roll No:153
Program
Name:Write a program to accept the string and
display it reverse string.
#include<stdio.h>
#include<conio.h>
void
main()
{
char
str[21];
int
i,j;
clrscr();
printf("\n
Enter the string-");
gets(str);
i=0;
while(str[i]!='\0')
{
i++;
}
i=i+1;
printf("\n The reverse string is-");
for(j=i;j>=0;j--)
{
printf("%c",str[j]);
}
getch();
}
/*Output
Enter the string-Maharashtra
The reverse string is-arthsatahaM */
4)Write
a program to accept the string and check whether it is palindrome or not.
Student
Name:Tambe
Sanjay Baban Roll No:153
Program
Name: Wap
to accept the string and check whether it is palindrome or not.
#include<stdio.h>
#include<conio.h>
void
main()
{
char
str[21];
int
i,j=0,temp=0;
clrscr();
printf("\n
Enter the string-");
gets(str);
i=0;
while(str[i]!='\0')
{
i++;
}
i--;
while(j!=i)
{
if(str[i]==str[j])
{
temp=1;
}
else
{
break;
}
j++;
i--;
}
if(temp==1)
{
printf("\n String is Palindrome");
}
else
{
printf("\n String is not Palindrome");
}
getch();
} /*
Output
Enter the string-madam Enter the string-India
String is
palindrome String is not palindrome */
5)1. Write
a menu driven program in 'c' which performs the following, operations on
string.
-check if one string is
substring of another string,
-count number of occurrences of
a character in the string,
-Exit.
Student
Name:Tambe
Sanjay Baban Roll No:153
program
name:write
a menu driven prodram in 'c' which performs the following operations on string
-cheak if one string is
substring of another string ,
-count number of occurrences of
acharacter in the string,
-Exit.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void
main()
{
int
ch;
char
str1[30],str2[30],occ;
int
i=0,cnt=0;
clrscr();
while(1)
{
printf("\n
menues");
printf("\n1.
check if one string is substring of another string-");
printf("\n2.
count number of occurrences of characters in the string-");
printf("\n3.exit");
printf("\nEnter
Your choice: ");
scanf("%d",&ch);
switch(ch)
{
case
1:printf("\n enter the first string-");
scanf("%s",str1);
printf("\n enter the second string-");
scanf("%s",str2);
if(strstr(str1,str2)==NULL)
{
printf("\n second string is not substring of first
string");
}
else
{
printf("\n second string is substring of first string");
}
break;
case
2:
printf("\n enter
the string-");
scanf("%s",&str1);
printf("\n enter the
character to find occurences");
scanf("%s",&occ);
i=0,cnt=0;
while(str1[i]!='\0')
{
if(str1[i]==occ)
{
cnt++;
}
i++;
}
printf("\n the number of occurencs is %d",cnt);
break;
case
3:exit(0);
}
}
getch();
}
/*
output
menues
1. check if one
string is substring of another string
2. count number
of occurrences of characters in the string
3.exit
Enter Your
choice: 1
enter the first string-india
enter the second string-ind
second string is substring of first string
menues
1. check if one
string is substring of another string
2. count number
of occurrences of characters in the string
3.exit
Enter Your
choice: 2
enter the string-Maharashtra
enter the
character to find occurencesa
the number of occurencs is 4
menues
1. check if one
string is substring of another string
2. count number
of occurrences of characters in the string
3.exit
Enter Your
choice: */