Converting Words To Pig Latin – Version 1 (C++)

Pig Latin in C++

One of my more popular blog posts is the one on converting words to pig latin using Python. As I’m finding my feet with C++ I am looking at reworking projects I have completed in Python into C++, with that in mind: Converting Words To Pig Latin.

geektechstuff_cpp_pig_latin_0
Making a start on user string input

I started the project by double checking my knowledge of arrays and input/output in C++, if you are only interested in the Pig Latin coding then skip to that heading below.

Generating An Array

char alphabet[27];

for (int i=0; i<26; i++) {

   alphabet[i] = i +97;

}

I first wanted to test creating and using an array, so started with creating an array of the alphabet. The above creates an array of 27 and then fills the array automatically by calling on the ASCII values (97 to 122) and a for loop.

alphabet[26]=0;

cout << alphabet << endl;

cout << alphabet[0] << endl;

cout << alphabet[25] << endl;

During this initial testing of my understanding of C++ loops and initialising arrays I am calling on some console output to check details.

User Input

string response; // creates a variable called response to hold a string

cout << "Enter a string" << endl;

getline(cin>>ws, response); // takes user input and stores it in the response variable

The program has a string variable called “response” and then asks the user to enter a string. I originally just used cin rather than getline, but during testing I was entering multiple words with upper / lower case changes in and noticed that cin ends the input when it detects a white space (ws). I’ll probably switch back to just cin for the initial program (taking one word at a time).

Converting To Lower Case

for (int i=0;i<response.length();i++){

 response[i]=tolower(response[i]);

}

cout << response << endl;

The program currently needs to convert the users inputted string into lower case. I am using a for loop against the length of the response variable and replacing the letters using the tolower function.

geektechstuff_cpp_pig_latin_1
Testing that the string returns in lower case and checking the length of the string

Pig Latin

geektechstuff_cpp_pig_latin_2
Pig Latin in C++

I have included comments in the C++ code to help remind myself and anyone reading it. If the word entered starts with a vowel then “yay” gets added to the end of the word. If the word entered starts with a consonant then the consonant is removed, added to the end of the word and then “ay” is added to the end of that new word.

#include <iostream>

// geektechstuff pig latin

using namespace std;

int main()

{

char consonant[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n','p', 'q', 'r','s', 't', 'y', 'v', 'x', 'z', '\0'};

char vowel[] = {'a', 'e', 'i', 'o', 'u', '\0'};

//Taking user input

string response; // creates a variable called response to hold a string

cout << "Enter a string" << endl;

getline(cin>>ws, response); // takes user input and stores it in the response variable

//Convert input to lower case

for (int i=0;i<response.length();i++){

 response[i]=tolower(response[i]);

}

cout<<"You typed: "<<response<<endl;

//check to see if first letter is a vowel

for (int i=0;i<5;i++){

  if (response[0] == vowel[i]){

        cout << "Your word starts with a vowel" << endl;

        cout << "In pig latin this word would be: "<<response<<"yay"<<endl;

        break;

       }

   }

// creating new array to hold consonant pig latin answer

int length = response.length();

length = length-1;

char new_response[length];

// check to see if first letter is a consonant

for (int i=0;i<21;i++){

   if (response[0]==consonant[i]){

        int new_response_index = 0;

        for (int x=1;x<response.length();x++){

            new_response[new_response_index]=response[x];

            new_response_index++;

            }

        new_response[length]='\0';

        cout<<"Your word starts with a consonant, in pig latin:  "<< new_response << response[0]<<"ay"<<endl;

        break;

       }

   }

}

I struggled to find a way to “pop” a value from an array in C++, which is how I tackled it in Python so instead I created a second array and moved the values needed into that array. I’m not too sure that it is the most efficient way of tackling the issue and may revisit it. On a side note; I also need to detect non-alphabet characters and warn the user not to use them.