Implement operations on Strings.
Source Code
#include<stdio.h>
#include<string.h>
void Insert(char*, char*, int);
void Delete(char*, int, int);
int Replace(char*, char*, char*);
int Find(char*, char*);
int main(){
char str[50],substr[20],newstr[20];
int index,choice,pos,len;
printf("Enter string:\n");
gets(str);
printf("::STRING OPERATIONS::\n1. Insertion\n2. Deletion\n3. Find Substring\n4. Replace\n5. Exit\n");
printf("Select string operation:\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter substring to insert:\n");
scanf("%c");
gets(substr);
printf("Enter position:\n");
scanf("%d",&pos);
if(pos<strlen(str)){
Insert(str,substr,pos);
printf("After insertion string is:\n%s\n",str);
}
else
printf("Insertion is not possible.\n");
break;
case 2:
printf("Enter position:\n");
scanf("%d",&pos);
printf("Enter length:\n");
scanf("%d",&len);
if(pos+len < strlen(str)){
Delete(str,pos,len);
printf("After deletion string is:\n%s\n",str);
}else{
printf("Cannot perform deletion operation.\n");
}
break;
case 3:
printf("Enter substring:\n");
scanf("%c");
gets(substr);
index = Find(str, substr);
if(index==-1)
printf("'%s' is not found in '%s'.\n",substr,str);
else
printf("'%s' is found in '%s' at %d position.\n",substr,str,index);
break;
case 4:
printf("Enter old string:\n");
scanf("%c");
gets(substr);
printf("Enter new string:\n");
gets(newstr);
index = Replace(str,substr,newstr);
if(index==-1)
printf("Replace is not possible.\n");
else
printf("After replace string is:\n%s\n",str);
break;
case 5:
exit(0);
default:
printf("Invalid choice.\n");
}
return 0;
}
void Insert(char* str, char* substr,int pos){
int i,j;
for(i=0;i<strlen(substr);i++){
for(j=strlen(str)+1;j>pos;j--)
str[j] = str[j-1];
}
for(i=0,j=pos;i<strlen(substr);i++,j++)
str[j] = substr[i];
}
void Delete(char* str,int pos,int len){
int i,j;
for(i=1;i<=len;i++){
for(j=pos;j<strlen(str);j++)
str[j] = str[j+1];
}
}
int Find(char* str, char* substr){
int i,j,k;
for(i=0;str[i]!='\0';i++){
for(k=i,j=0;substr[j]!='\0';j++,k++){
if(str[k]!=substr[j])
break;
}
if(j==strlen(substr)){
return i;
}
}
if(i==strlen(str))
return -1;
}
int Replace(char* str,char* oldstr,char* newstr){
int oldstrpos;
oldstrpos = Find(str,oldstr);
if(oldstrpos==-1)
return -1;
else{
Delete(str,oldstrpos,strlen(oldstr));
Insert(str,newstr,oldstrpos);
}
}
#include<string.h>
void Insert(char*, char*, int);
void Delete(char*, int, int);
int Replace(char*, char*, char*);
int Find(char*, char*);
int main(){
char str[50],substr[20],newstr[20];
int index,choice,pos,len;
printf("Enter string:\n");
gets(str);
printf("::STRING OPERATIONS::\n1. Insertion\n2. Deletion\n3. Find Substring\n4. Replace\n5. Exit\n");
printf("Select string operation:\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter substring to insert:\n");
scanf("%c");
gets(substr);
printf("Enter position:\n");
scanf("%d",&pos);
if(pos<strlen(str)){
Insert(str,substr,pos);
printf("After insertion string is:\n%s\n",str);
}
else
printf("Insertion is not possible.\n");
break;
case 2:
printf("Enter position:\n");
scanf("%d",&pos);
printf("Enter length:\n");
scanf("%d",&len);
if(pos+len < strlen(str)){
Delete(str,pos,len);
printf("After deletion string is:\n%s\n",str);
}else{
printf("Cannot perform deletion operation.\n");
}
break;
case 3:
printf("Enter substring:\n");
scanf("%c");
gets(substr);
index = Find(str, substr);
if(index==-1)
printf("'%s' is not found in '%s'.\n",substr,str);
else
printf("'%s' is found in '%s' at %d position.\n",substr,str,index);
break;
case 4:
printf("Enter old string:\n");
scanf("%c");
gets(substr);
printf("Enter new string:\n");
gets(newstr);
index = Replace(str,substr,newstr);
if(index==-1)
printf("Replace is not possible.\n");
else
printf("After replace string is:\n%s\n",str);
break;
case 5:
exit(0);
default:
printf("Invalid choice.\n");
}
return 0;
}
void Insert(char* str, char* substr,int pos){
int i,j;
for(i=0;i<strlen(substr);i++){
for(j=strlen(str)+1;j>pos;j--)
str[j] = str[j-1];
}
for(i=0,j=pos;i<strlen(substr);i++,j++)
str[j] = substr[i];
}
void Delete(char* str,int pos,int len){
int i,j;
for(i=1;i<=len;i++){
for(j=pos;j<strlen(str);j++)
str[j] = str[j+1];
}
}
int Find(char* str, char* substr){
int i,j,k;
for(i=0;str[i]!='\0';i++){
for(k=i,j=0;substr[j]!='\0';j++,k++){
if(str[k]!=substr[j])
break;
}
if(j==strlen(substr)){
return i;
}
}
if(i==strlen(str))
return -1;
}
int Replace(char* str,char* oldstr,char* newstr){
int oldstrpos;
oldstrpos = Find(str,oldstr);
if(oldstrpos==-1)
return -1;
else{
Delete(str,oldstrpos,strlen(oldstr));
Insert(str,newstr,oldstrpos);
}
}
Enter string:
Hello how are you?
::STRING OPERATIONS::
1. Insertion
2. Deletion
3. Find Substring
4. Replace
5. Exit
Select string operation:
2
Enter position:
6
Enter length:
3
After deletion string is:
Hello are you?
Hello how are you?
::STRING OPERATIONS::
1. Insertion
2. Deletion
3. Find Substring
4. Replace
5. Exit
Select string operation:
2
Enter position:
6
Enter length:
3
After deletion string is:
Hello are you?