Question

In: Computer Science

Write a program in C that lets the user enter a message using SMS Language(e.g. lol,...

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

Solutions

Expert Solution

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();
}


Related Solutions

How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
C++ Write a program that lets the user enter a two letters which are f and...
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program. Example: Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times. Output2: The second most repeating...
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT