In: Computer Science
Write a program in C that lets the user enter a message using SMS Language(e.g. lol, omg, omw etc.), then translates it into English (e.g. laughing out loud, oh my god, on my way etc.). Also provide a mechanism to translate text written in English into SMS Language.
needs to be able to translate at least 10 sms words
UPDATED CODE. P
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *replaceWord(const char *string, const char *oldWord,
const char *newWord)
{
char *result;
int i, cnt = 0;
int newWordlen = strlen(newWord);
int oldWordlen = strlen(oldWord);
for (i = 0; string[i] != '\0'; i++)
{
if (strstr(&string[i], oldWord) == &string[i])
{
cnt++;
// Jumping to index after the old word.
i += oldWordlen - 1;
}
}
// Making new string of enough length
result = (char *)malloc(i + cnt * (newWordlen - oldWordlen) +
1);
i = 0;
while (*string)
{
// compare the substring with the result
if (strstr(string, oldWord) == string)
{
strcpy(&result[i], newWord);
i += newWordlen;
string += oldWordlen;
}
else
result[i++] = *string++;
}
result[i] = '\0';
return result;
}
char* getMessage(char sms[10][5],char message[10][30],char
*input){
int i=0;
for(i=0;i<10;i++){
if(strstr(input,sms[i])!=NULL){
char* result
=replaceWord(input, sms[i], message[i]);
input=result;
}
}
return input;
}
char* getSMS(char sms[10][5],char message[10][30],char
*input){
int i=0;
for(i=0;i<10;i++){
if(strstr(input,message[i])!=NULL){
char* result
=replaceWord(input, message[i], sms[i]);
input=result;
}
}
return input;
}
void main(){
char
sms[10][5]={"R","OMG","L8","2DAY","U","2","PLS","PPL","GAS","FTL"};
char message[10][30]={"ARE","OH MY
GOD","LATE","TODAY","YOU","TO","PLEASE","PEOPLE","GREETINGS AND
SALUTATIONS","FOR THE LOSS"};
int i=0;
int choice=1;
char input[100];
while(choice!=3){
printf("Please choose :\n1: SMS to
Message.\n2: Message to SMS.\n3:Exit\n");
scanf("%d",&choice);
fflush(stdin);
if(choice==1){
printf("Please
give SMS :");
scanf("%[^\n]",input);
strcpy(input,getMessage(sms,message,input));
printf("%s\n",input);
}else if(choice==2){
printf("Please
give SMS :");
scanf("%[^\n]",input);
strcpy(input,getSMS(sms,message,input));
printf("%s\n",input);
}else if(choice==3){
printf("Program
Ended.\n");
}else{
printf("Invalid
Choice.\n");
}
}
getchar();
}