写一个`subString()`函数

时间:2015-03-03 16:51:58

标签: c string

  

编写一个名为subString()的函数来提取字符串的一部分。该函数应按如下方式调用

subString (source, start, count, result); 
     

其中source是从中提取子字符串的字符串,start是源中的索引号,表示子字符串的第一个字符,count是要从源字符串中提取的字符数,结果是要包含提取的子字符串的字符数组。

这是我到目前为止所拥有的。当我运行程序时,我得到了一个奇怪的结果

#include <stdio.h>

char substring (char source[], int start, int count, char result[])
{
int i;
    source[start] = result [0]; // set the initial value of the result array     to the value of the source array at the point of 'start'

    for (i = 1; i <= count; i++, start++) {         // keep adding to start     until 'result' has obtained all the values it was supposed to get from the     source array
        source[start] = result[i];
    }
    result [count+1] = '\0';    // adds a null terminator to the end of the resultant string
    return *result;
}

int main (void)
{
    char source [] = "Any help would be appreciated. I dont know where I've gone     wrong"; // define a generic string for the user to extract a string from
    int start = 0, count = 0;
    char result [255];


// allows for multiple different calls to be made without having to adjust the code every time
    printf("%s",source);

    printf("\n\nAt what point in the above statement should extracting begin? "); // assigns a starting point
    scanf("%d", &start);

    printf("\n\nHow many characters do you want to extract? "); // sets a number of characters to extract
    scanf("%d", &count);

    substring (source, start, count, result); // call the substring function

    printf("\n\nThe resultant string, after extraction is: %s\n\n", substring); // print the result of the subString function

    return 0;
}

2 个答案:

答案 0 :(得分:1)

这是对代码问题的有组织的枚举

  1. 您正在将函数名称传递给printf()。这表明这是一个糟糕的函数名称选择,您应该通过result而不是substring

  2. 您只返回result函数中substring()的第一个字符。

  3. 您的函数使用未初始化的数组result作为源字符串,这是不正确的,您需要将字符串从source复制到result

  4. 这是一个建议

    char *substring (char *source, int start, int count, char *result)
    {
        int i;
    
        for (i = 0 ; ((i < start) && (source[i] != '\0')) ; i++)
            result[i] = source[i];
        for ( ; source[i + count] != '\0' ; i++)
            result[i] = source[i + count];
        result[i] = '\0';
    
        return result;
    }
    

    我认为很容易理解这个功能的作用。

    非常重要:在c数组中将0编入索引N - 1,因此请注意for循环。

答案 1 :(得分:-1)

我使用上面的一些帮助,但我也有很多自己的想法。感谢大家的帮助

#include <stdio.h>

char *substring (char *source, int start, int count, char *result)
{
    int i;

    for (i = 0 ; i < (count) ; i++) 
        result[i] = source[i + start]; // after hitting start, as long as you're not at the end, the result should match the source
    result[i] = '\0';

    return result;
}
int main (void)
{
    char source [] = "character"; // define a generic string for the user to extract a string from
    int start = 0, count = 0;
    char result [255];

printf("%s",source);

printf("\n\nAt what point in the above statement should extracting begin? "); // assigns a starting point
scanf("%d", &start);

printf("\n\nHow many characters do you want to extract? "); // sets a number of characters to extract
scanf("%d", &count);

substring (source, start, count, result);

printf("\n\nThe resultant string, after extraction is: %s\n\n", result); // calling result, not the whole function

return 0;
}