从C中的命令行复制String

时间:2016-04-06 06:29:12

标签: c

此文件编译并运行但有错误。这是我大部分完成的家庭作业,但它没有正确输出。我并没有要求对我的程序进行重新设计,而是对我做错了什么有用的暗示。输出中的错误是每个其他单词搞砸了,我无法弄清楚原因。谢谢你的帮助。

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

#define SIZE 20

void plural(char word[]);

int main(int argc, char *argv[])
{
    if(argc >= 2)
    {
    int i;
        for(i=1; i<argc; i++)
        {
            printf("noun: %s\n",argv[i]);
            plural(argv[i]);
            printf("plural: %s\n\n", argv[i]);
        }   
    }
    else
    {
        printf("ERROR: You must pass the nouns to be pluralized as program arguements\n");
    }

    return 0;
}

void plural(char word[])
{
    /* declarations */  
    int length;

    /* find length of word */
    length = strlen(word);

    /* check first rule:  if word ends in "y" then change to "ies" */
    if (word[length - 1] == 'y') {
        word[length - 1] = 'i';
        word[length] = 'e';
        word[length + 1] = 's';
        word[length + 2] = '\0';   /* put '\0' at end of string */
    }

    /* check second rule:  if word ends in "s" "ch" or "sh" add "es" */
    else if (word[length - 1] == 's' ||
        (word[length - 2] == 'c' && word[length - 1] == 'h') ||
        (word[length - 2] == 's' && word[length - 1] == 'h'))
    {
        /* concatenate "es" to word */
        strcat(word, "es");
    }

    /* otherwise, just add "s" to the end of word */
    else 
    {
        strcat(word, "s");
    } 

}

3 个答案:

答案 0 :(得分:3)

每个strlen(argv[i]) + 1都分配了malloc个内存。您编写了更多字符,这些字符会进入无效的内存位置,从而调用未定义的行为。

修复是根据需要从函数和free内存创建指针,返回指针并main#include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 20 /* Note: SIZE is unused */ char* plural(char word[]); int main(int argc, char *argv[]) { if(argc >= 2) { int i; for(i = 1; i < argc; i++) { printf("noun: %s\n", argv[i]); char* str = plural(argv[i]); printf("plural: %s\n\n", str); free(str); } } else { printf("ERROR: You must pass the nouns to be pluralized as program arguements\n"); } return 0; } char* plural(char word[]) { /* declarations */ int length; char* str; /* find length of word */ length = strlen(word); /* check first rule: if word ends in "y" then change to "ies" */ if (word[length - 1] == 'y') { str = malloc(length + 3); strcpy(str, word); strcpy(str + length - 1, "ies"); } /* check second rule: if word ends in "s" "ch" or "sh" add "es" */ else if (word[length - 1] == 's' || (length > 1 && word[length - 2] == 'c' && word[length - 1] == 'h') || (length > 1 && word[length - 2] == 's' && word[length - 1] == 'h')) { str = malloc(length + 3); strcpy(str, word); /* concatenate "es" to word */ strcat(str, "es"); } /* otherwise, just add "s" to the end of word */ else { str = malloc(length + 2); strcpy(str, word); strcat(str, "s"); } return str; } 开始。

请参阅此代码(未经测试)

var f = document.frmSubmit;    

        var radios = document.getElementsByName('appbk')

	    for (var i = 0; i < radios.length; i++) {
	        if (document.getElementById("appbk") == false) {
	        alert('Remarks has exceeded allowable radio characters.');
	        f.appbk.focus();
	        return false; // checked
	    }
	    }

        if (f.FEEDBACK_MSG.value != "" ) { 
	        if (f.FEEDBACK_MSG.value.length > 500 ){			
				bootbox.alert('Remarks has exceeded allowable maximum characters.');
				f.FEEDBACK_MSG.focus();
				return false;
			} 
		}

        return true;
        document.getElementById("btnsubmit").disabled = true;
    }

答案 1 :(得分:2)

您不应该使用argv参数列表,就好像它们是可修改的字符串一样。最重要的是,你不知道为它们分配的内存超出了它们的长度,所以你不能使用strcat这样的东西,它会创建数组超出范围的bug。

相反,在修改每个此类参数之前,请复制它。

答案 2 :(得分:2)

你不能在字符串length之外写字。 word[length + 1]word[length + 2]错误。您应该使用一个新的更大的字符串来存储复数。

您可以简单地将argv复制到一个新的更大的字符串中:

#define SIZE 128

void plural(char word[]);

int main(int argc, char *argv[])
{
    char plural_string[SIZE] = {0};

    if(argc >= 2)
    {
        int i;

        for(i=1; i<argc; i++)
        {
            printf("noun: %s\n",argv[i]);
            strncpy(plural_string, argv[i], SIZE);
            plural(plural_string);
            printf("plural: %s\n\n", plural_string);
        }   
    }
    else
    {
        printf("ERROR: You must pass the nouns to be pluralized as program arguements\n");
    }

    return 0;
}

另请注意,复数函数必须考虑字符串的最小允许长度。word[length - 2]可以将数组索引越界。