The program should change a sentence to pig Latin.
Rules
A word starting with a vowel should start with a capital letter and the rest with small letters and end with "way".
If a word starts with a consonant the first letter will move to the last until it starts with a vowel then make the First letter capital and the rest small letters and finally ends the word with "ay"
Example
PLEASE hElp Me I NeEd You
output:
Easeplay Elphay Emay Iway Eednay Ouyay
Main.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"function.h"
main(){
char word[100];
input(word);
divider(word);
}
function.h
void input(char string[]){ //gets a string
printf("input a string:");
gets(string);
}
void letter(char string[]){ //translates word/sentence to pig latin
char cons[100];
char vows[100];
int i=0,j=0,a,x,c,k,q=0,d;
strcpy(vows,"aeiouAEIOU");
strcpy(cons,"bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ");
for(i=0;i<strlen(vows);i++){ //checks IF THE WORD STARTS WITH A vowel
if(string[0]==vows[i]){
string[0]=toupper(string[0]); //changes first letter to capital
for(k=1;k<strlen(string);k++){ //changes the rest to lower case
string[k]=tolower(string[k]);
}
strcat(string,"way"); //then adds the word "way"
break; //stops the operation
}
}
for(d=0;d<strlen(cons);d++){ //checks the consonants
do{
for(j=0;j<strlen(cons);j++){
if(string[0]==cons[j]){ //if the word starts with a consonant
strncat(string,string,1); //adds the first letter to the last
string[0]=' '; //removes the first letter
a=0;
for(a=0;a<=strlen(string);a++){ //allocates the location of individual string
string[a]=string[a+1];
}
for(x=0;x<strlen(vows);x++){
if(string[0]==vows[x]){ //checks if the first letter has become a vowel
string[0]=toupper(string[0]); //changes first case to upper
for(c=1;c<strlen(string);c++){
string[c]=tolower(string[c]);
} //changes rest of the case to lower
strcat(string,"ay"); //then it adds the word "ay"
break; //stops the operation
}
}
break;
}
}
}while(string[0]==cons[d]); //runs as long as the first letter is a consonant
}
}
void output(char string[]){ //shows the printed output
printf("%s\n",string);
}
void divider(char string[]){
char delims[] = " ";
char *result = NULL;
result = strtok(string,delims);
while(result != NULL){
letter(result); //my problem
output(result);
result = strtok(NULL,delims);
}
}
Errors
Single words and sentences that start with vowels continuously prints "Ayway" then Segmentation Fault.
Sentences that starts with consonants just prints the first word then idles
Comments:
The code works Fine on single words when I remove while statement in the divider function.