C 尝试删除单词的第一个字符并将其放在末尾
第一次发布到此站点。 我正在尝试编写一个pig拉丁翻译程序,但在删除字符串中每个单词的第一个字符并将其附加到单词末尾时遇到了困难。如果有人能给我任何建议,我将不胜感激。然而,我试图不改变我已经拥有的太多。就字符串函数而言,我仅限于使用strcpy、strcmp、strlen和strtok,因为我只是一名综合课程的学生C 尝试删除单词的第一个字符并将其放在末尾,c,localization,apache-pig,C,Localization,Apache Pig,第一次发布到此站点。 我正在尝试编写一个pig拉丁翻译程序,但在删除字符串中每个单词的第一个字符并将其附加到单词末尾时遇到了困难。如果有人能给我任何建议,我将不胜感激。然而,我试图不改变我已经拥有的太多。就字符串函数而言,我仅限于使用strcpy、strcmp、strlen和strtok,因为我只是一名综合课程的学生 #include <stdio.h> #include <string.h> void main (void) { char sentence[81];
#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语句[81];/*保存输入字符串*/
char*platin;/*将指向每个单词*/
printf(“此程序翻译句子中的单词。\n”);
printf(“键入结束到结束。\n”);
每个句子都要做/**/
{
printf(“\n\n键入一个句子,直到'stop':\n”);
得到(句子);
platin=strtok(句子“”);
while(platin!=NULL)/*将翻译器从一个词移动到另一个词*/
{
if(strchr(“aeiouAEIOU”,*platin))/*检查元音*/
{
printf(“%sway”,铂);
}
否则如果(strchr(“BCDFGHJKLMNPqrSTVWxyZBCDFGHKLmNPqrSTVxyZ”、*platin))
{
printf(“%say”,铂);
}
platin=strtok(空,“”);
}
}while(strcmp(句子“停止”)!=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
}
char[1024]wordBuff;
int j=0;
for(int i=0;i