C program to convert text to Pig Latin

Just a fun program I wrote the other day, mostly to exercise and maintain my C programming skills. It works for the most part, though it only takes lowercase sentences without any punctuation, and I haven’t figured out how to get it to work with words where the first vowel is “y”.

This program was inspired by a program in /usr/games in my Slackware installation, which was also supposed to convert sentences to Pig Latin, but for some reason didn’t work at all (meaning I would give it a sentence and it would just do nothing).

 

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main( int argc, char **argv ){
        int pos = 0;
        int len = strlen( argv[1] );
        while( pos <= len ){
                char str1[len + 2], str2[len + 2];
                forint i = 0; i < len + 2; i++ ){
                        str1[i] = '\0';
                        str2[i] = '\0';
                }
                // Copy the next word:
                int j = 0;
                while( isalpha( argv[i][pos] ) ){
                        str1[j++] = argv[1][pos++];
                }
                // Convert the word to Pig Latin:
                forint i = 0; i < len; i++ ){
                        if( str1[i] == 'a' || str1[i] == 'e' || str1[i] == 'i' || str1[i] == 'o' || str1[i] == 'u' ){
                                strcpy( str2, str1 + i ); // Copy part after first vowel
                                strncpy( str2 + strlen( st1 ) - i, str1, i ); // Copy part before first vowel
                                strcpy( str2 + strlen( str1 ), "ay" );
                                break;
                        }
                }
                // Print Pig Latin word:
                print( "%s ", str2 );
                // Advance to next word:
                while( isspace( argv[1][pos] ) ){
                        pos++;
                }
                if( argv[1][pos] == ā€˜\0ā€™ ) break;
        }
        printf( "\n" );
        return 0;
}

 

Advertisements