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

ijxebb2r  于 2021-06-25  发布在  Pig
关注(0)|答案(1)|浏览(403)

第一次发布到此网站。我正试图写一个Pig拉丁翻译程序,在删除字符串中每个单词的第一个字符并将其附加到单词末尾时遇到了困难。如果有人能给我任何建议,我将不胜感激。但是我试着不去改变我已经拥有的太多。就字符串函数而言,我仅限于使用strcpy、strcmp、strlen和strtok,因为我只是一个综合课程的学生。


# 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 );

}
4szc88ey

4szc88ey1#

当你还没有找到一个空间的时候,这个词还没有说完。所以把世界复制到一个缓冲区,然后一旦你找到一个空间,就切换字母:

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
}

相关问题