unix - Echo All Palindromes, in C -


I like the views presented in the book "Unix Programming Environment" by Brian Kernighan and Rob Pike, where they focus on the point To work within an environment where you can put many (small, accurate, well understood) programs together on the command line to complete many programming tasks.

I brush on the strict ANSI conferences and I am trying to stick to this philosophy somewhere else in this book (if necessary, I can get the exact page number), they suggest that this environment All programs should follow the following principles:

  1. If the input is presented on the command line, the process of input, as an argument for the program.

  2. If input is not provided on the command line, input input from stdin. Here is a program that I wrote that it resonates any input (numeric or alphabetical), which is a palladium, especially my question:

    >

    Is this a good practice program? In other words, is it that Kerning and Pike were suggesting that such a command line is the optimum behavior for the app?

      #include & lt; Stdio.h & gt; #include & lt; String.h & gt; * * RR_Straing [100] for / * Strlen * / int main (int argc, char * argv []); If (argc> 1) {int length = (int) strlen (argv [1]); Int i = 0; Int j = length; R_string [j] = (four) faucet; J--; For (i = 0; i & lt; length; i ++, j--) {r_string [j] = argv [1] [i]; } If (strancoma (arguably [1], r_string) == 0} {printf ("% s \ n", argv [1]); }} And {four * i_string; While (Scan ("% s", i_string)! = EOF) {int length = (int) strlen (i_string); Int i = 0; Int j = length; R_string [j] = (four) faucet; J--; For (i = 0; i & lt; length; i ++, j--) {r_string [j] = i_string [i]; } If (strcmp (i_string, r_string) == 0) {printf ("% s \ n", i_string); }} 0 return 0; }  

Yes, I think you are following the advice of R & As Hugo said, you can take the logic name a file name, bu, and TMHO for this simple program, I can say that taking parameters as a parindrom can only be more understood.

In addition to this, if you give me additional advice, I will separate the functionality of reading a string whether it is an illusion or not because you have this code duplicated now

 < Code> Intl Ezelindrome (Const char * c) {size_t len ​​= strlen (c); Size_t limit = lane / 2; Size_t i; Break {if (c [i]! = C [len-i-1]) for (i = 0; i & lt; border; i ++); / * Individual characters get I / / * If we have reached the limit, then this is a penile error. /  

Of course, I'm pretty sure this improvement can be done (It may also be a bug, I am writing very fast), but once you have the string, either from command line or user input, you can call this function or function like this Are there.

Note: Edit by Mark to reflect the comments, many thanks, Mark!


Comments