【问题标题】:Looping name backwards in C在C中向后循环名称
【发布时间】:2015-07-06 01:36:01
【问题描述】:

我想从 n 次 (n = 1-100) 向后打印我的名字。我似乎无法弄清楚。正如您在我的代码中看到的那样,strrev() 在循环时不起作用,因为它不断地反复反转。我还想以某种方式强制用户输入一个字母而不是数字作为名称,但这不是必须的。

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

int main () 
{
    int choice;
    char name[255];
    int loopCounter;
    int count;
    int number;

    do 
    {
        printf("1.Display Your Name\n");
        printf("2.Display Your Name (From 1-100)\n");
        printf("3.Display your Name Backwards\n");
        printf("4.Display Your Name Backwards (From 1-100)\n");
        printf("5.Quit\n");

        printf("Enter your name:\n");
        scanf("%s", name);

        printf("Enter your choice:   ");
        scanf("%i", &choice);

        switch(choice)
        {
            case 1: 
                printf("Your name is %s.\n", name);
                break;
            case 2:  
                printf("How many times to display your name? (1-100) ");
                scanf("%i", &count);

                if ( 100 < count) 
                {
                    printf("Please enter 1 through 100\n");
                }
                else 
                {
                    loopCounter = 0;
                    while (loopCounter < count ) 
                    {
                        loopCounter++;
                        printf("%d %s\n", loopCounter,name);
                    }
                }
                break;
            case 3:
                printf("Your name backwards is: %s\n\n", strrev(name));
                break;
            case 4:
                printf("How many times to display your name backwards? ");
                scanf("%i", &count);
                if ( 100 < count) 
                {
                    printf("Please enter 1 through 100\n");
                }
                else 
                {
                    loopCounter = 0;
                    while (loopCounter < count ) 
                    {
                        loopCounter++;
                        printf("%d %s\n", loopCounter, strrev(name));
                    }
                }
                break;
           case 5:
               printf("Goodbye!!!!\n");
               break;
           default:
               printf("Was not 1 through 5\n");
               break;
       }
   } while (choice!=5);
}   

【问题讨论】:

  • 你的文本编辑器是一只愤怒的猫吗?

标签: c arrays string loops


【解决方案1】:

您的问题是 strrev() 将字符串反转到位。

如果您想保留原件,您应该复制名称并在循环之前使用strrev() 将其反转一次。然后在循环中,只需调用反向名称。

否则,只需这样做:

loopCounter = 0;
strrev(name);
while (loopCounter < count ) {
    loopCounter++;
    printf("%d %s\n", loopCounter, name);
}

【讨论】:

    【解决方案2】:

    还有另一个解决方案不需要strrev.. 观看以下代码:-- 主要()

    {
    char name[255];
    int loopcounter;
      for(loopcounter = strlen(name)-1; loopcounter>=0; loopcounter--)
      {
         printf("%c", name[loopcounter]);
      }
    }
    

    这段代码肯定会对你有所帮助....好的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 2021-12-03
      • 2017-11-21
      • 2023-01-21
      • 2022-11-15
      • 2019-06-14
      相关资源
      最近更新 更多