STRING FUNCTION USING C
To write a c program to manipulate the string following the operations.
i. Concatenating two string
ii. Reversing the string
iii. Finding substring
iv. Replace the string
v. Finding the length of string.
/*STRING FUNCTION */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int ch;
void concat();
void rever();
void len();
void replc();
void substr();
clrscr();
printf("\n\n\tSTRING OPERATIONS");;
printf("\n\t********************\n");
do
{
printf("\n\t1.CONCATINATION\n\t2.REVERSE\n\t3.LENGHT\n\t4.REPLACE\n”);printf(“\t5.SUBSTRING");
printf("\n\t6.EXIT");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
concat();
break;
case 2:
rever();
break;
case 3:
len();
break;
case 4:
replc();
break;
case 5:
substr();
break;
case 6:
exit(0);
break;
default:
printf("\n\tInvaid choice");
}
}
while(ch!=6);
getch();
}
void concat()
{
int i=0,j=0;
char s1[30],s2[30];
printf("\nEnter string1:");
scanf("%s",s1);
printf("\nEnter string2:");
scanf("%s",s2);
while(s 1[i]!='\0')
i++;
while(s 2[j]!='\0')
s1[i++]=s2[j++];
s1[i]='\0';
printf("\nThe concate string:%s",s1);
getch();
}
void rever()
{
char s[30];
int i=0,j;
printf("\nEnter the string:");
scanf("%s",s);
while(s[i]!='\0')
i++;
printf("\nThe reverse string is:");
for(j=i;j>=0;j--)
printf("%c",s[j]);
getch();
}
void len()
{
char s[30];
int i=0;
printf("\nEnter the string:");
scanf("%s",s);
while(s[i]!='\0')
i=i+1;
printf("\nThe length of string is:%d",i);
getch();
}
void replc()
{
int i,pos;
char s1[30],s2[30];
printf("\nEnter the string1:");
scanf("%s",s1);
printf("\nThe replacement string: ");
scanf("%s",s2);
printf("\nThe replace position:");
scanf("%d",&pos);
pos=pos-1;
i=0;
while(s1[i]!='\0')
s1[pos++]=s2[i++];
printf("\nThe result after replacement:%s",s1);
getch();
}
void substr()
{
int i,k,c;
char s1[30];
printf("\nString1:");
scanf("%s",s1);
printf("\nThe starting index:");
scanf("%d",&i);
printf("\nEnter the number of character:");
scanf("%d",&c);
printf("The substring is:");
for(k=i;k<c+i-1;k++)
printf("%c",s1[k]);
getch();
}
OUTPUT:
STRING OPERATIONS
********************
1.CONCATINATION
2.REVERSE
3.LENGHT
4.REPLACE
5.SUBSTRING
6.EXIT
Enter your choice: 1
Enter string1: vivek
Enter string2: anand
The concate string: vivekanand
1.CONCATINATION
2.REVERSE
3.LENGHT
4.REPLACE
5.SUBSTRING
6.EXIT
Enter your choice: 2
Enter the string: dog
The reverse string is: god
1.CONCATINATION
2.REVERSE
3.LENGHT
4.REPLACE
5.SUBSTRING
6.EXIT
Enter your choice: 3
Enter the string: vivekananthan
The length of string is: 13
1.CONCATINATION
2.REVERSE
3.LENGHT
4.REPLACE
5.SUBSTRING
6.EXIT
Enter your choice: 4
Enter the string1: vivek
The replacement string: i
The replace position: 2
The result after replacement: vi
1.CONCATINATION
2.REVERSE
3.LENGHT
4.REPLACE
5.SUBSTRING
6.EXIT
Enter your choice: 5
String1: vivek
The starting index: 2
Enter the number of character: 3
The substring is: ve
1.CONCATINATION
2.REVERSE
3.LENGHT
4.REPLACE
5.SUBSTRING
6.EXIT
Enter your choice: 6
Comments
Post a Comment