呼应所有回文,在C [英] Echo All Palindromes, in C

查看:110
本文介绍了呼应所有回文,在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我爱的想法$在布赖恩Kernighan和罗伯派克的书psented p $的UNIX编程环境,他们专注于一个环境中工作的地步,你可以放在一起许多(小,precise很好理解)在命令行程序来完成许多编程任务。

我严格的ANSI C约定刷牙,并试图坚持这一理念。某处在这本书(我可以得到,如果需要一个确切的页码),他们认为,在这种环境中的所有程序应遵循以下原则:


  1. 如果输入psented在命令行上$ P $,作为参数传递给程序本身,过程的输入。


  2. 如果没有输入psented在命令行上$ P $,从标准过程输入。


下面是我写的,这将呼应任何输入(数字或字母)一个C程序,它是一个回文。我的问题具体是:

这是一个很乖的C程序?换句话说,这是什么Kernighan和派克曾建议是这样吗?

命令行应用的最佳行为

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT; / *函数strlen * /INT主(INT ARGC,CHAR *的argv []){    烧焦r_string [100];    如果(argc个大于1){        INT长度=(INT)strlen的(的argv [1]);        INT I = 0;
        INT J =长度;
        r_string [J] =(CHAR)NULL;
        j--;
        对于(i = 0; I<长度;我+ +,j--){
           r_string [J] =的argv [1] [I]
        }        如果(的strcmp(的argv [1],r_string)== 0){
            的printf(%S \\ N的argv [1]);
        }    }其他{        字符* i_string;        而(scanf函数(%S,i_string)!= EOF){            INT长度=(INT)strlen的(i_string);            INT I = 0;
            INT J =长度;
            r_string [J] =(CHAR)NULL;
            j--;
            对于(i = 0; I<长度;我+ +,j--){               r_string [J] = i_string [I]
            }            如果(STRCMP(i_string,r_string)== 0){                的printf(%S \\ n,i_string);
            }
        }
    }    返回0;
}


解决方案

是的,我认为你是继R&放大器; K-建议。正如雨果说,你可以采取argumentas一个文件名,卜,T恕我直言,这个简单的程序,我会说,服用参数作为回文本身可能更有意义。

另外,如果你让我额外的建议,我会分开,从检查它是否是一个回文或不读一个字符串的功能,因为你有code复制现在。

  INT ispalindrome(为const char * C){
   为size_t LEN = strlen的(C);
   为size_t极限= len个/ 2;
   为size_t我;
   对于(i = 0; I<限制;我++){
     如果(三[I] = C [len个-I-1]!)打破; / *不同的性格找到* /
   }
   回到我==限制; / *如果我们达到了极限,这是一个palyndrome * /
}

当然,我是pretty肯定,这可以改善(它甚至可能有一个bug,我typping相当快),但一旦你有你的字串,无论是从命令行或用户输入,则可以调用此功能,或者像这样的functiom。

请注意:编辑,以反映马克评论,非常感谢,马克

I love the ideas presented in Brian Kernighan and Rob Pike's book, "The UNIX Programming Environment," where they focus on the point of working within an environment where you can put together many (small, precise, well understood) programs on the command line to accomplish many programming tasks.

I'm brushing up on strict ANSI C conventions and trying to stick to this philosophy. Somewhere in this book (I can get an exact page number if needed) they suggest that all programs in this environment should adhere to the following principles:

  1. If input is presented on the command line, as an argument to the program itself, process that input.

  2. If no input is presented on the command line, process input from stdin.

Here's a C program I wrote that will echo any input (numeric or alphabetic) that is a palindrome. My question specifically:

Is this a well behaved C program? In other words, is this what Kernighan and Pike were suggesting is the optimal behavior for a command line application like this?

#include <stdio.h>
#include <string.h> /* for strlen */

int main(int argc, char* argv[]) {

    char r_string[100];

    if (argc > 1) {

        int length = (int)strlen(argv[1]);

        int i = 0;
        int j = length;
        r_string[j] = (char)NULL;
        j--;
        for (i = 0; i < length; i++, j--) {
           r_string[j] = argv[1][i];
        }

        if (strcmp(argv[1], r_string) == 0) {
            printf("%s\n", argv[1]);
        }

    } else {

        char* i_string;

        while (scanf("%s", i_string) != EOF) {

            int length = (int)strlen(i_string);

            int i = 0;
            int j = length;
            r_string[j] = (char)NULL;
            j--;
            for (i = 0; i < length; i++, j--) {

               r_string[j] = i_string[i];
            }

            if (strcmp(i_string, r_string) == 0) {

                printf("%s\n", i_string); 
            }
        }
    }

    return 0;
}

解决方案

Yes, I think that you are following the R&K advice. As Hugo said, you could take the argumentas a filename, bu,t IMHO, for this simple program, I'd say that taking the parameter as the palindrome itself may make more sense.

Also, if you allow me extra advice, I would separate the functionality of reading a string from checking whether it is a palindrome or not, because you have that code duplicated right now.

int ispalindrome(const char* c) { 
   size_t len = strlen(c);
   size_t limit = len/2;
   size_t i;
   for (i = 0; i < limit; i++) {
     if(c[i]!=c[len-i-1]) break; /* Different character found */
   }
   return i==limit; /* If we reached limit, it's a palyndrome */
}

Of course, I am pretty sure this can be improved (it may even have a bug, I am typping quite fast), but once that you have your string, be either from command line or user input, you can call this function or a functiom like this.

NOTE: Edited to reflect comment from Mark, thanks a lot, Mark!

这篇关于呼应所有回文,在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆