my objective is to change the first vowel of each word in a sentence into 'i' and move the initial consonant cluster (if there is) to the end of the word and add ee.
for example:
- string = "flower yellow"
- translated string = "iwer-flee illow-yee"
so far, the code i have rn is able to split the original string into words so that the strchr function finds the vowel in each word and replace it into 'i'. But the vowel being replaced is not the first vowel. Instead, it only follows the order of vowels listed in the vowels array.
- string: flower yellow
- translated string: flowir yillow
it should be fliwer yillow.
void englishToMatte(char englishSentence[], char matteSentence[])
{
char** word = splitIntoWords(englishSentence);
char *p;
int vowels[] = {'a', 'e', 'i', 'o', 'u'};
int i;
char** it;
char dest[100];
for(it=word; it && *it; it){
for(i = 0; i < 5; i ){
if((p = strchr(*it, vowels[i])) != NULL){
*p = 'i';
break;
}
}
strcat(dest, *it);
strcat(dest, " ");
free(*it);
}
/*puts(dest);*/ /*for debugging*/
strcpy(matteSentence, dest);
free(word);
}
I think I should iterate through the *it string from start to end or replace the if condition into a switch case instead but i don't know how i would be able to include strchr in the switch parameter. I know there must be an answer to this already but so far I only found too specific answers like this: How to iterate through a char ** variable
or c programs that gives the same undesired results: https://quescol.com/interview-preparation/replace-first-vowel-program-in-c
i know this is a fairly easy problem to solve but i am an absolute noob.
CodePudding user response:
in case whoever's reading this have the same problem, here's the portion of the code (much thanks to gerhardh)
for(it=word; it && *it; it){
myString=*it;
for(int i=0; (*it)[i]!=0; i ){
if(myString[i]=='a' ||myString[i]=='e' ||myString[i]=='i' ||myString[i]=='o' ||myString[i]=='u'){
myString[i] = 'i';
break;
}
}
strcat(dest, *it);
strcat(dest, " ");
free(*it);
}
now the remaining problem is to append the initial consonant cluster to end of the word. will be updating this when i find the solution.
CodePudding user response:
This should approximate what you seem to want. Cannot be sure about the inserted hypen, or not.
Count the words, allocate space for result, process each word according to location of first vowel (if present in "word") building output result. Note: this will not account for multiple spaces separating words in original.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *smpl = "Quick brown foxes jump over flower yellow & Unicorns";
char *wordSep = " ";
int main() {
int len = strlen( smpl );
printf( "Orginal string: '%s' (%d chars)\n", smpl, len );
// Count the words in the "sentence"
char *pBuf = strdup( smpl );
int nWord = 0;
for( char *cp = pBuf; (cp = strtok( cp, wordSep ) ) != NULL; cp = NULL )
nWord ;
printf( "%d \"words\"\n", nWord );
strcpy( pBuf, smpl); // reload
// create output buffer large enough for expansion.
char *oBuf = (char*)calloc( len nWord*4 1, sizeof(char) );
// calloc NEVER returns null pointer
char *at = oBuf;
for( char *pWord = pBuf; (pWord = strtok( pWord, wordSep ) ) != NULL; pWord = NULL ) {
char *init = pWord; // beginning of word
char *vwl1 = strpbrk( init, "AEIOUaeiou" ); // first vowel of word
if( vwl1 == NULL ) { // all consonants!
at = sprintf( at, "%s ", init );
continue;
}
if( vwl1 == init ) { // eg: "Unicorn"
*at = "iI"[ isupper( *vwl1 ) ]; // 'i' matching the case
at = sprintf( at, "%s-ee ", init 1 );
continue;
}
*at = "iI"[ isupper( *vwl1 ) ]; // 'i' matching the case
*vwl1 = '\0';
at = sprintf( at, "%s-%see ", vwl1 1, init );
}
puts( oBuf );
free( oBuf );
free( pBuf );
return 0;
}
Output:
Orginal string: 'Quick brown foxes jump over flower yellow & Unicorns' (52 chars)
9 "words"
iick-Qee iwn-bree ixes-fee imp-jee iver-ee iwer-flee illow-yee & Inicorns-ee
As an exercise, work out how to make the 'i' match the case of uppercase 'Q' at the start of the sentence (and make the relocated 'Q' lowercase...)