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], cData;
int i=0,s=0,rep,sel,x,eq, lastline = 0, j=0, k=0;
FILE *pFile;
char *tmp;
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){
int j=0, s=0;
if((pFile = fopen("strings.txt", "r+t")) != NULL){
//get all the strings in the file
while ((cData = fgetc(pFile)) != EOF){
if(cData != '\n'){//||cData!='\r\n'||cData!='\r'||cData!=NULL){
string[s][j] = cData;
j++;
}else{
string[s][j]='\0';
j=0;
s++;
}
}
}else{
pFile = fopen("strings.txt", "wt");
}
for(i=0; i<s; i++){
printf("%s", string[i]);
}
do{
system("cls");
printf("Input a string: ");
scanf("%s",&string[s]);
system("cls");
for(j=0; j<strlen(string[s]); j++){
fputc(string[s][j], pFile);
}
fputc('\n', pFile);
printf("\nString %s successfully added\n", string[s]);
printf("Enter another string?\n\n");
printf("1 - Yes\n");
printf("2 - No\n");
scanf("%d",&rep);
s++;
for(i=0; i<s; i++){
printf("%s", string[i]);
}
}while(rep==1 && s<16);
fclose(pFile);
}
else if(sel==2){
do{
system("cls");
printf("Select position of string to remove:\n");
int j=0, i=0;
if ((pFile = fopen("strings.txt", "r+t")) != NULL){
while ((cData = fgetc(pFile)) != EOF){
if(cData != '\n'){
string[i][j] = cData;
j++;
}else{
string[i][j]='\0';
j=0;
i++;
}
}
}
fclose(pFile);
int k=0;
for(k=0;k<i;k++)
printf("%d - %s\n",k+1,string[k]);
scanf("%d",&x);
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");
int j=0, i=0;
if ((pFile = fopen("strings.txt", "r+t")) != NULL){
while ((cData = fgetc(pFile)) != EOF){
if(cData != '\n'){
string[i][j] = cData;
j++;
}else{
string[i][j]='\0';
j=0;
i++;
}
}
}
fclose(pFile);
int k=0;
for(k=0;k<i;k++)
printf("%d - %s\n",k+1,string[k]);
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);
printf("EQ %d",eq);
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);
}


