do while循环中的字符串

时间:2014-12-10 02:24:09

标签: c

// PigLatinFinal.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>

void Heading (void)
{
    printf("************************************************************\n");
    printf("*                        Pig Latin                         *\n");
    printf("*                                                          *\n");
    printf("*                                                          *\n");
    printf("*                     Welcome                              *\n");
    printf("*                                                          *\n");
    printf("* In this program, the user will enter a word in English   *\n");
    printf("* The program will then return the word in Pig Latin.      *\n");
    printf("* The word cannot be longer than 7 letters.  You can       *\n");
    printf("* translate as many words as you wish. Enter 'END' without *\n");
    printf("* the quotation marks to end the program. 'END is case     *\n");
    printf("* sensitive                                                *\n");
    printf("*                                                          *\n");
    printf("************************************************************\n");
    printf("\n\n\n\n");
}

int main(void)
{
    Heading();
    char str1[20];
    int i =0;
    do
        {

        int count =0;
        do
            {          
            printf("Enter a word in English for the program to return in Pig Latin: ");
            scanf("%s", str1);
            while(str1[count] != '\0')
            ++count;
            if (count>7)
                {
                printf("\n\nInvalid Entry. Please enter a word with no more than 7 letters\n\n");
                count=0;
                }
            }while (count>7);//loop to perform if invalid word is entered

//我输入大于7个字母的单词,它仍然用猪拉丁语给出它,而不是提示另一个单词。我尝试放入一个while循环,但它只是给出一个无限循环

         //while (count<7)
         //
        printf("\n%s in Pig Latin: ",str1);
        for (i=1;str1[i];++i)
            {
            printf("%c",str1[i]);
            }
        printf("%c",str1[0]);
        printf("ay");
        printf("\n\n\n");
         //}


    }while(str1!= "END"); 

//此外,它不会通过输入END结束。我究竟做错了什么?

return 0;
    }

1 个答案:

答案 0 :(得分:0)

更改

}while (count>7);//loop to perform if invalid word is entered

}while (count==0);//loop to perform if invalid word is entered

}while(str1!= "END");

}while(strcmp(str1,"END")!=0);

另外,请勿忘记添加string.h