试图删除单词的第一个字符并将其放在最后

时间:2013-11-14 03:01:53

标签: c localization apache-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 );

}

1 个答案:

答案 0 :(得分: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
}