Friday, 14 September 2012

C++ Program for Pig Latin Converter

C++ Program for  Pig Latin Converter


#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>

int makePhrase(char * word, char phrase[][20]);

void main()
{
clrscr();
   char word[30] = {'\0'};
   char phrase[20][20];
   char convert[30] = {'\0'};
   char temp[10] = {'\0'};
   int counter =0;
   int check = 0;
   int firstvowel = 0;
   int i;
   int numcons;

   cout<<"Please enter a word or phrase up to 30 characters"<<endl;
   cin.getline(word,30,'\n');
   strlwr(word);
   cout<<"\nYour entry was '"<<word<<"'"<<endl;
   cout<<"---------------------------"<<endl;

   i=makePhrase(word,phrase);

   for (int k=0; k<i; k++)
   {
   int size = strlen(phrase[k]);

   for(int j=0;j<size;j++)
      {

      switch(phrase[k][j])
      {
      case 'a':
     if (j == 0)
        firstvowel = 1;

     check = 1;
     break;
      case 'e':
     if (j == 0)
        firstvowel = 1;

     check = 1;
     break;
      case 'i':
     if (j == 0)
        firstvowel = 1;

     check = 1;
     break;
      case 'o':
     if (j == 0)
     firstvowel = 1;

     check = 1;
     break;
      case 'u':
     if (j == 0)
        firstvowel = 1;

     check = 1;
     break;
      default:
     if (check != 1 && firstvowel != 1)
     counter++;
     break;


      }


   };
      if (firstvowel == 1)
      {
      strcat(phrase[k],"hay");
      cout<<"Word begins with a vowel, no need for movement"<<endl;
      cout<<"Your word in Pig Latin is\n"<<phrase[k]<<endl;
      cout<<"---------------------------"<<endl;
      }

      else
      {
      numcons = counter-1;
      cout<<"The first vowel is: "<<phrase[k][counter]<<endl;
      cout<<"The letter(s) to move is:"<<endl;

      do
      {                               cout<<phrase[k][numcons]<<endl;
                temp[numcons] = phrase[k][numcons];
      numcons-=1;
      }while(numcons !=-1);

      int leftover = strlen(phrase[k]) - numcons;

      for(int i=0; i<leftover; i++)

      {
      convert[i] = phrase[k][counter];
      counter+=1;
      };
      strcat(convert, temp);
      strcat(convert, "ay");
      strcpy(phrase[k],convert);
cout<<"Your word in Pig Latin is:\n"<<phrase[k]<<endl;
cout<<"---------------------------"<<endl;
   };
   numcons=0;
   counter=0;
   firstvowel=0;
   size=0;
   check = 0;
   strcpy(temp,"0");
   };



        cout<<"\n\nYour translated entry:\n";
   cout<<"--------------\n";
   for(int a=0;a<i;a++)
   {
      cout<<phrase[a]<<" ";
   };
   cout<<"\n--------------"<<endl;
};

int makePhrase(char * word, char phrase[][20])
{
int i=0;
char *space = " ";
char *breaker;
breaker = strtok(word,space);
   while ( breaker )
   {
   strcpy( phrase[i],breaker );
   breaker = strtok( NULL, space );
    i++;
   };
   return i;
};

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More