Can someone help me to convert the if and elseif statements to switch-case format and i want to store my strings to a text file how will i do that?
Code:
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main(){
char string[16][128],temp[128],search[128];
int i=0,s=0,rep,sel,x,eq;
do{
do{
system("cls");
printf("String Database\n\n");
printf("1 - Store a String to the Database\n");
printf("2 - Remove a String from the Database\n");
printf("3 - View Strings in the Database\n");
printf("4 - Search a String in the Database\n");
printf("5 - Exit\n");
scanf("%d",&sel);
}while(sel!=1&&sel!=2&&sel!=3&&sel!=4&&sel!=5);
if(sel==1){
do{
system("cls");
printf("Input a string: ");
scanf("%s",&string[s]);
s++;
do{
system("cls");
printf("String successfully added\n");
printf("Enter another string?\n\n");
printf("1 - Yes\n");
printf("2 - No\n");
scanf("%d",&rep);
}while(rep!=1&&rep!=2);
}while(rep==1);
}
else if(sel==2){
do{
system("cls");
printf("Select position of string to remove:\n");
for(i=0;i<s;i++)
printf("%d-%s\n",i+1,string[i]);
scanf("%d",&x);
strcpy(string[x-1],"");
s--;
strcpy(temp,string[s]);
for(i=x;i<s;i++)
strcpy(string[i-1],string[i]);
strcpy(string[s-1],temp);
do{
system("cls");
printf("String successfully deleted\n");
printf("Remove another string?\n\n");
printf("1 - Yes\n");
printf("2 - No\n");
scanf("%d",&rep);
}while(rep!=1&&rep!=2);
}while(rep==1);
}
else if(sel==3){
system("cls");
for(i=0;i<s;i++)
printf("%d - %s\n",i+1,string[i]);
printf("\nPress any key to return to main menu");
getch();
}
else if(sel==4){
do{
eq=1;
strcpy(search,"");
system("cls");
printf("Enter string to search: ");
scanf("%s",&search);
for(i=0;i<s;i++){
eq=strcmp(string[i],search);
if(eq==0)
break;
}
do{
system("cls");
if(eq==0){
printf("String matched\n");
printf("Position of searched string in array is %d",i+1);
}
else{
printf("String not found\n");
printf("Search another string?\n\n");
printf("1 - Yes\n");
printf("2 - No\n");
scanf("%d",&rep);
}
}while(rep!=1&&rep!=2);
}while(rep==1);
}
}while(sel!=5);
}