This program must be written in c++, and full points go to the answer that works
ID: 643166 • Letter: T
Question
This program must be written in c++, and full points go to the answer that works and has the best explanation for each line of code.
Write a program that changes an English sentence into "pig Latin". Use the algorithm: To form a pig-latin phrase from an English phrase, tokenize the phrase into words with the function strtok (or other means if you prefer). To translate each English word into a pig-latin word, place the first letter of the English word at the end of the English word and add the letters "ay". You may assume no punctuation marks and that all words have two or more letters.
Explanation / Answer
#include <stdio.h>
#include <string.h>
void printLatinWord( char *word ); // function prototype
int main( void )
{
char sentence[ 80 ]; // sentence input by user
char *tokenPtr; // pointer to current token
puts( "Enter a sentence:" );
fgets( sentence, 80, stdin );
puts( " The sentence in pig Latin is:" );
// call function strtok to alter the sentence
tokenPtr = strtok( sentence, " " );
// if tokenPtr does not equal NULL
while ( tokenPtr ) {
// pass the token to printLatinWord and get next token
printLatinWord( tokenPtr );
tokenPtr = strtok( NULL, " " );
// if tokenPtr not NULL, print space
if ( tokenPtr ) {
printf( "%s", " " );
} // end if
} // end while
puts( "." );
} // end main
// print out the English word in pig Latin form
void printLatinWord( char *word )
{
size_t i; // loop counter
// loop through the word
for ( i = 1; i < strlen( word ); ++i ) {
printf( "%c", word[ i ] );
} // end for
printf( "%c%s", word[ 0 ], "ay" );
} // end function printLatinWord
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.