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 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 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...
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...
In Java:Implement a program that repeatedly asks the user to input apositive integer and...
In Java:Implement a program that repeatedly asks the user to input a positive integer and outputs the factorial of that input integer. Do not use recursion, solution should use stack.
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
Using RAPTOR create a program that allows the user to input a list of first names...
Using RAPTOR create a program that allows the user to input a list of first names in on array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. the output should be a list of email address where the address is of the following form: [email protected]
Create program which verifies if input string is a valid variable declaration or not. Use C...
Create program which verifies if input string is a valid variable declaration or not. Use C programming language. - This program can only use the following variable types: char, float, and int - Remove any newline \n from input string - The input prompt should say ">>> " - If input declaration is valid, it should print "Valid dec\n" - If input declaration is invalid, it should print "Invalid dec\n" - Make sure the identifier entered matches the rules of...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT