gpt4 book ai didi

c - 尝试删除单词的第一个字符并将其放在末尾

转载 作者:行者123 更新时间:2023-11-30 17:44:30 26 4
gpt4 key购买 nike

第一次在这个网站发帖。我正在尝试编写一个 pig 拉丁语翻译程序,但很难删除字符串中每个单词的第一个字符并将其附加到单词的末尾。如果有人能给我任何建议,我将不胜感激。然而我试图不去改变我已经拥有的太多。就字符串函数而言,我仅限于使用 strcpy、strcmp、strlen 和 strtok,因为我只是综合类(class)中的一名被难住的学生。

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

void main (void)
{
char sentence[81]; /* holds input string */
char *platin; /* will point to each word */

printf ("This program translate the words in your sentence.\n");
printf ("Type end to finish.\n");

do /* for each sentence */
{
printf ("\n\nType a sentence until 'stop': \n ");
gets (sentence);

platin = strtok (sentence, " ");
while (platin != NULL) /*Moves translator from word to word */
{

if (strchr("aeiouAEIOU", *platin)) /*Checks for vowels */
{

printf(" %sway ", platin);
}

else if (strchr("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ",*platin))
{
printf(" %say", platin);
}




platin = strtok(NULL, " ");



}
} while (strcmp(sentence, "stop") != 0 );

}

最佳答案

虽然你还没有找到空格,但这个词还没有结束。因此,将世界复制到缓冲区中,然后一旦找到空格,就交换字母:

char[1024] wordBuff;
int j = 0;
for (int i = 0; i < strlen(sentence); i++) {
if (sentence[i] == ' ') {
char tmpC = wordBuff[j-1]; //
wordBuff[j-1] = wordBuff[0]; // switch the letters
wordBuff[0] = tmpC; //
wordBuff[j] = '\0'; // end of word
printf("%s\n", wordBuff);
j = 0;
}
else
wordBuff[j++] = sentence[i]; // fill wordBuff with word's char
}

关于c - 尝试删除单词的第一个字符并将其放在末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19968709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com