我的目標是將句子中每個單詞的第一個元音更改為“i”并將初始輔音簇(如果有)移動到單詞的末尾并添加 ee。
例如:
- string = "花黃色"
- 翻譯后的字串 = "iwer-flee illow-yee"
到目前為止,我擁有的代碼 rn 能夠將原始字串拆分為單詞,以便 strchr 函式找到每個單詞中的元音并將其替換為“i”。但是被替換的元音不是第一個元音。相反,它只遵循元音陣列中列出的元音順序。
- 字串:花黃色
- 翻譯字串:flowir 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);
}
我想我應該從頭到尾遍歷 *it 字串,或者將 if 條件替換為 switch case,但我不知道如何將 strchr 包含在 switch 引數中。我知道必須已經有了答案,但到目前為止,我只找到了這樣的太具體的答案: 如何迭代 char ** 變數
或給出相同不良結果的 c 程式: https ://quescol.com/interview-preparation/replace-first-vowel-program-in-c
我知道這是一個相當容易解決的問題,但我絕對是菜鳥。
uj5u.com熱心網友回復:
這應該近似于您似乎想要的。不能確定是否插入了連字符。
計算單詞,為結果分配空間,根據第一個元音(如果存在于“單詞”中)的位置處理每個單詞,構建輸出結果。注意:這不會考慮原始單詞中的多個空格分隔。
#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;
}
輸出:
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
作為練習,找出如何使“i”匹配句子開頭的大寫“Q”(并使重新定位的“Q”小寫......)
uj5u.com熱心網友回復:
萬一讀這篇文章的人有同樣的問題,這里是代碼的一部分(非常感謝 gerhardh)
dest[100] = " "; //initialize string
//see main post above for unmentioned variables
for(it=word; it && *it; it){
myString=*it;
for(int i=0; myString[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(myString);
}
現在剩下的問題是將初始輔音簇附加到單詞的末尾。當我找到解決方案時會更新這個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/506498.html
上一篇:為什么地址不一樣?
下一篇:此代碼如何生成記憶體對齊切片?
