In: Computer Science
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.
#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;
}