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...
Write a program in c++ using only if statements that prompts the user to enter an...
Write a program in c++ using only if statements that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1 …., and Saturday is 6) then displays today. Also, prompt the user to enter the number of days after today for a future day and display the future day of the week. The future day can be computed as follows: (today + number of days after today) % 7 Sample run...
Write a program that lets the user enter the loan amount, number of years, and interest...
Write a program that lets the user enter the loan amount, number of years, and interest rate and displays the amortization schedule for the loan. Steps: 1) Create scanner 2) Prompt the user to enter loan amount and declare double variable and relate to scanner input 3) Prompt the user to enter number of years and declare integer years and relate to scanner input 4) Prompt the user to enter annual interest rate and declare double variable and relate to...
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...
Write a java program that lets the user to enter any two integers and weave them...
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT