清理argv程序

我有一个argv c程序,它会反转这个词,并且看到它是否是argv,我似乎无法弄清楚如何去做 . int main(int argc,char * argv []){char string =(char)malloc(1000);
string [0] = '\0';

for(i = 1; i < argc; i++)                                                                        
    strcat(string, argv[i]);

    for(j = 0; string[ j ]; j++)
    string[j] = tolower(string[ j ]);

    reverse(string);
    printf("Reverse of entered word is \"%s\".\n", string);

    result = is_palindrome(string);

    if ( result == 1)
        printf("\"%s\" is a palindrome.\n", string);
    else
        printf("\"%s\" is not a palindrome.\n", string); 

    system("pause");
}

我遗漏了其余的功能,但你可以在主要部分看到printf显示它是否是回文 . 不知道如何使其等于原始输入,并知道参数的数量 . 目前它打印反转的字符串,当我认为打印原件会更好看 . 我想我太难了,但我不确定 .

回答(3)

2 years ago

显而易见的方法是反转反向字符串,另一种方法是使用双引号将单个参数/字符串传递给程序:

./program "a man a plan a canal panama"

代替

./program a man a plan a canal panama

然后你不需要遍历args .

2 years ago

我看到两个问题:参数的数量在 argc 中 - 参见例如this question了解 argcargv[] 的详细信息 .

关于如何打印原始字符串,您可以在调用 reverse() 之前打印字符串,将原始字符串存储在临时变量中以便稍后输出或在打印之前再次反向输出 . 第三个选项看起来像这样:

int main(int argc, char* argv[])
{
    char *string = (char*)malloc(1000);                                                              
    string[0] = '\0';                                                                                

    for(i = 1; i < argc; i++)                                                                        
    strcat(string, argv[i]);

    for(j = 0; string[ j ]; j++)
    string[j] = tolower(string[ j ]);

    reverse(string);
    printf("Reverse of entered word is \"%s\".\n", string);

    result = is_palindrome(string);

    if ( result == 1)
        printf("\"%s\" is a palindrome.\n", reverse(string));
    else
        printf("\"%s\" is not a palindrome.\n", reverse(string)); 

    system("pause");
}

2 years ago

UPDATE 因为OP说教授会输入句子(没有引号!) .

这可以使用MSVC库函数简单地完成 . 因为 strdup 分配了内存,所以也需要清理 .

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

void fatal(char *msg);

int main(int argc, char *argv[]) {
    int i, argslen = 0;
    char *inp, *rev;
    if (argc < 2)                           // check num args
        fatal("Need a string argument");
    for (i=1; i<argc; i++)                  // figure input length
        argslen += strlen(argv[i]) + 1;     // include spaces and EOS
    inp = malloc(argslen);                  // memory for concatenation
    if (inp == NULL)
        fatal("Memory allocation error");
    strcpy(inp, argv[1]);                   // prime with first arg
    for (i=2; i<argc; i++) {                // concat other args
        strcat(inp, " ");
        strcat(inp, argv[i]);
    }

    rev = strdup(inp);                      // make a copy
    if (rev == NULL)
        fatal("Memory allocation error");
    strrev(rev);                            // reverse the copy
    printf("Testing: \"%s\"\n", inp);
    if (stricmp(inp, rev) == 0)             // case insensitive test
        printf("Is a palindrome\n");
    else
        printf("Not a palindrome\n");
    free (inp);                             // release mine
    free (rev);                             // release library's
    return 0;
}

void fatal(char *msg) {
    printf("%s\n", msg);
    exit (1);
}

计划结果:

>test Able was I ere I saw Elba
Testing: "Able was I ere I saw Elba"
Is a palindrome