Thursday, November 27, 2014

C program for string operations(reverse,compare,occurence)

/*..........................................................................................................................................
File Name : rec20.c
Program : Write the following function of subprogram using pointers and use it in main program with a menu option:
 Check whether a given character exists in a string or not
 Comparing two string lexicographically
 Count the occurrence of a given character in a string
 Reverse the given string.
Author : sujithbaby440@gmail.com
Date : 21-11-2014
………………………………………………………………………………………………...*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void fnexist(char *s,char ch);
void fnlex(char *s, char *t);
void fncount(char *s,char ch);
void fnrev(char *s);
main()
{
int choice;
char *s,*t,ch;
s=(char *)malloc(25);
t=(char *)malloc(25);
system("clear");
printf("Program using pointer and use in main program\n");
printf("1.Check whether the given character exists in string\n");
printf("2.Compare two strings lexiographically\n");
printf("3.Count the occurrence of a given character in a string\n");
printf("4.Reverse the given string\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice){
case 1:printf("Enter string\n");
scanf("%s",s);
getchar();
printf("Enter character\n");
scanf("%c",&ch);
fnexist(s,ch);
break;
case 2:printf("Enter first string\n");
scanf("%s",s);
getchar();
printf("Enetr second string\n");
scanf("%s",t);
fnlex(s,t);
break;
case 3:printf("Enter string\n");
scanf("%s",s);
getchar();
printf("Enter character\n");
scanf("%c",&ch);
fncount(s,ch);
break;
case 4:printf("Enter string\n");
scanf("%s",s);
getchar();
fnrev(s);
break;
default:printf("Invalid choice\n");
break;
}
}
void fnexist(char *s, char ch)
{
int f=0,i=0;
while(*(s+i)!='\0'){
if(*(s+i)==ch){
f=1;
break;
}
i++;
}
if(f==1){
printf("Character found\n");
}else{
printf("Charcter not found\n");
}
}
void fnlex(char *s, char *t)
{
int f=1,i=0;
while(*(s+i)!='\0'){
if(*(s+i)!=*(t+i)){
f=0;
break;
}
i++;
}
if(f==1){
printf("Both strings are equal\n");
}else{
printf("Strings are not equal\n");
}
}
void fncount(char *s, char ch)
{
int f=0,i=0;
while(*(s+i)!='\0'){
if(*(s+i)==ch){
f++;
}
i++;
}
printf("Character occur %d times in string\n",f);
}
void fnrev(char *s)
{
int i=0,j=0,len;
char temp;
while(*(s+i)!='\0'){
i++;
}
len=i;
for(i=0;i<len/2;i++){
temp=*(s+i);
*(s+i)=*(s+len-i-1);
*(s+len-i-1)=temp;
}
printf("Reverse of string\n");
printf("%s",s);
printf("\n");
}

OUTPUT 1
Program using pointer and use in main program
1.Check whether the given character exists in string
2.Compare two strings lexiographically
3.Count the occurrence of a given character in a string
4.Reverse the given string
Enter your choice:
1
Enter string
tomorrow
Enter character
r
Character found
[141740@localhost ~]$


OUTPUT 2
Enter your choice:
4
Enter string
string
Reverse of string
gnirts
[141740@localhost ~]$
OUTPUT 3
Enter your choice:
2
Enter first string
triangle
Enter second string
triangle
Both strings are equal
[141740@localhost ~]$


OUTPUT 4
Enter your choice:
3
Enter string
occurrence
Enter character
c
Character occur 3 times in string
[141740@localhost ~]$

No comments:

Post a Comment