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.

You need to learn how to use your compiler's debugger so that you can single step through your program and find out for yourself what is causing the problem. That will save you lots and lots of time

Your code is terribly difficult to follow, and is very unstructured spaghetti code. Not to mention the style of formatting used is just plain ridiculous. I am not trying to be rude, but rather give you some really good advice that can help you pinpoint problems like this in the future. The best thing I can do to help you out is to tell you is to go back to the drawing board and start over. This is like trying to find a needle in a haystack.

Producing code that "just works" is not a good idea, by this I mean you should never use a break statement in anything other than a switch, repetition statements should not be used in that way (this is most likely where the problem lies, in one of your loops).
There are 5 lower case and 5 capital vowels for example, and you are using strcpy to store these in an array that has been allocated for 100, and this is very wasteful, not relevant at the moment, but will be in your future endeavors.

Consider something like this :

char cons[] = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
      char vows[] = "aeiouAEIOU";

www.jgrasp.org - this is a java based IDE that you can use for C code. It has a CSD generation tool that will automatically clean up K&R or BSD styles formatting styles, and debugging is really really easy.

Major problem #1
Executable code does NOT go in a header file. They go in .C file
Variable definitions do NOT go in a header file. They go in .C file

I'd fix these items first.

Thanks everyone for the replies especially to Stazloz who pointed out all the flaws of my program and gave tips for coding

Ive found that the "easiest" solution to the problem is to store the words that were tokenized and then use them for the letter function

..Someone should have told me that there is an issue with strtok and strcat..

Well anyway Thanks again

..Someone should have told me that there is an issue with strtok and strcat..

There are issues with almost all standard C functions, not just those two.

..Someone should have told me that there is an issue with strtok and strcat..

Since when is not knowing how to use a function an "issue" with the function? That's more like an issue with the programmer.

...by this I mean you should never use a break statement in anything other than a switch

I agree with most of the stuff you said, but certainly not this. You should avoid using a break statement in for loops when you can, but there are an awful lot of cases in the real world where it's more clean or even more efficient to use break statements in for loops.

commented: agree +17

Since when is not knowing how to use a function an "issue" with the function? That's more like an issue with the programmer.

Okay then let me rephrase what ive said..
..Someone should have told me "my mistake" using strtok and strcat..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.