Question

In: Computer Science

Using C++ Create a program that asks the user to input a string value and then...

Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form.

- If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”.

- If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the first character of the string to the end of the string until the first character of the string becomes a vowel. Then add the string "ay" at the end. For example, the Pig Latin form of the string "There" is "ere-Thay".

- Strings such as "by" contain no vowels. In cases like this, the letter y can be considered a vowel. So, for this program, the vowels are a, e, i, o, u, y, A, E, I, O, U, and Y. Therefore, the Pig Latin form of "by" is "y-bay".

- Strings such as "1234" contain no vowels. The pig Latin form of the string "1234" is "1234-way". That is, the pig Latin form of a string that has no vowels in it is the string followed by the string "-way". The program should store each character of a string into a linked list and use the rotate function, which removes the first item and puts it at the end of the linked list. The linked list should be implemented as a generic type.

Solutions

Expert Solution

  • #include <stdio.h>
    #include <string.h>


    // this function returns 1 if first character of the string is vowel
    // and returns 0 if first character is not vowel
    int isFirstCharVowel(char *s){
       // Check if first character is vowel
       if (s[0]=='a' || s[0]=='e' || s[0]=='i' || s[0]=='o' || s[0]=='u' || s[0]=='y' || s[0]=='A' || s[0]=='E' || s[0]=='I' || s[0]=='O' || s[0]=='U' || s[0]=='Y')
       {
           return 1;
       }
       return 0;
    }

    // this function returns 1 if any character of the string is vowel
    // and returns 0 if none of the character is vowel
    int isVowelPresent(char *s){
       int length=strlen(s);
       // Traverse the string to check vowel at every position
       for (int i = 0; i < length; ++i)
       {
           // If vowel is present return true
           if (s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='y' || s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U' || s[i]=='Y')
           {
               return 1;
           }
       }
       // If we have reached this point means vowels is not present hence return 1
       return 0;
    }

    // This function rotates string by one character
    void rotate(char *s){
       int length=strlen(s);
       char first=s[0];
       for (int i = 0; i < length-1; ++i)
       {
           s[i]=s[i+1];
       }
       s[length-1]=first;
    }

    int main(int argc, char const *argv[])
    {
       char s[100];
       printf("Please enter the string.\n");
       gets(s);
       // Calculate lenth of the string using strlen() function
       int length=strlen(s);

       // Check if the first character is vowel
       if (isFirstCharVowel(s))
       {
           s[length]='-';
           s[length+1]='w';
           s[length+2]='a';
           s[length+3]='y';
           s[length+4]='\0';
       }
       // If first character is not vowel
       else{
           // Check if string contains vowel at any position
           if (isVowelPresent(s))
           {
               // add - at the end of string
               s[length]='-';
               s[length+1]='\0';
               while(!(s[0]=='a' || s[0]=='e' || s[0]=='i' || s[0]=='o' || s[0]=='u' || s[0]=='y' || s[0]=='A' || s[0]=='E' || s[0]=='I' || s[0]=='O' || s[0]=='U' || s[0]=='Y')){
                   rotate(s);
               }
               length=strlen(s);
               // Add ay at the end of string
               s[length]='a';
               s[length+1]='y';
               s[length+2]='\0';
           }
           // If none of the string's character is vowel
           else{
               // Add -way at the end of string
               s[length]='-';
               s[length+1]='w';
               s[length+2]='a';
               s[length+3]='y';
               s[length+4]='\0';
           }
       }

       printf("%s\n", s);
      
       return 0;
    }


Related Solutions

Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
Write a C++ program using separate void which asks the user to input side of a...
Write a C++ program using separate void which asks the user to input side of a square, radius of a circle , height and base of a triangle and finds the area of squares, circles and triangles. Then using main function display the area of square, circle and triangle
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Create a class that calls for user to input a string. Pass the string from that...
Create a class that calls for user to input a string. Pass the string from that class to another and count how many words the string has, count how many vowels are in the string and count how many letters end with d. java language
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT