首页 > 解决方案 > 从 C 中的源字符串的开头提取子字符串时出错

问题描述

这是来自 C 第 4 版编程第 9 章中的一个练习。该程序是将字符读入字符串,并通过指定起始位置和字符数将字符串的一部分提取为子字符串。程序编译和运行良好,除非源的第零位被声明为开始。然后什么都没有显示。这是我的代码。

/* Programme to extract a portion from a string using function
sub-string (source, start, count, result) ex9.4.c

ALGORITHM
Get text input into a char array (declare to be fixed size);
Determine length of source string;
Prepare result array to be dynamic length using desired count + 1;
Copy from source array into result array

*/

#include <stdio.h>
#include <stdbool.h>

#define MAX 501

void read_Line (char buffer[]);
int string_Length (char string[]);
void sub_String (char source[], int start, int count, char result[]);

int main(void)
{
    char strSource[MAX];
    bool end_Of_Text = false;
    int strCount = 0;
    printf("This is a programme to extract a sub-string from a source string.\n");


    printf("\nType in your text (up to 500 characters).\n");
    printf("When you are done, press 'RETURN or ENTER'.\n\n");

    while (! end_Of_Text)
    {
        read_Line(strSource);

        if (strSource[0] == '\0')
        {
            end_Of_Text = true;
        }
        else
        {
            strCount += string_Length(strSource);
        }
    }

    // Declare variables to store sub-string parameters
    int subStart, subCount;
    char subResult[MAX];

    printf("Enter start position for sub-string: ");
    scanf(" %i", &subStart);
    getchar();

    printf("Enter number of characters to extract: ");
    scanf(" %i", &subCount);
    getchar();

    // Call sub-string function
    sub_String(strSource, subStart, subCount, subResult);

    return 0;

}

// Function to get text input
void read_Line (char buffer[])
{
    char character;
    int i = 0;

    do
    {
        character = getchar();
        buffer[i] = character;
        ++i;
    }
    while (character != '\n');

    buffer[i - 1] = '\0';
}

// Function to count determine the length of a string
int string_Length (char string[])
{
    int len = 0;

    while (string[len] != '\0')
    {
        ++len;
    }

    return len;
}

// Function to extract substring
void sub_String (char source[], int start, int count, char result[])
{
    int i, j, k;

    k = start + count;

    for (i = start, j = 0; i < k || i == '\0'; ++i, ++j)
    {
        result[j] = source[i];
    }

    result[k] = '\0';

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

我在 Linux Mint 上使用 Code::Blocks。

标签: arrayscstringfunction

解决方案


作为最近刚开始使用 CS50 和“C 语言编程”书籍学习编程的人,我不知道如何在 Code::Blocks 中设置调试器。但是由于@paulsm4 的推动,我设法让调试器工作了。使用调试器的监视窗口,我可以看到 main 函数中的 while 循环正在用空字符覆盖源数组中的第一个字符。解决方法是添加一个 break 语句。感谢@WhozCraig 和@Pascal Getreuer 指出了我错过的其他错误。这是现在更正的代码:

/* Programme to extract a portion from a string using function
sub-string (source, start, count, result) ex9.4.c

ALGORITHM
Get text input into a char array (declare to be fixed size);
Determine length of source string;
Prepare result array to be dynamic length using desired count + 1;
Copy from source array into result array

*/

#include <stdio.h>
#include <stdbool.h>

#define MAX 501

void read_Line (char buffer[]);
int string_Length (char string[]);
void sub_String (char source[], int start, int count, char result[]);

int main(void)
{
    char strSource[MAX];
    bool end_Of_Text = false;
    int strCount = 0;
    printf("This is a programme to extract a sub-string from a source string.\n");


    printf("\nType in your text (up to 500 characters).\n");
    printf("When you are done, press 'RETURN or ENTER'.\n\n");

    while (! end_Of_Text)
    {
        read_Line(strSource);

        if (strSource[0] == '\0')
        {
            end_Of_Text = true;
        }
        else
        {
            strCount += string_Length(strSource);
        }
        break;
    }

    // Declare variables to store sub-string parameters
    int subStart, subCount;
    char subResult[MAX];

    printf("Enter start position for sub-string: ");
    scanf(" %i", &subStart);
    getchar();

    printf("Enter number of characters to extract: ");
    scanf(" %i", &subCount);
    getchar();

    // Call sub-string function
    sub_String(strSource, subStart, subCount, subResult);

    return 0;

}

// Function to get text input
void read_Line (char buffer[])
{
    char character;
    int i = 0;

    do
    {
        character = getchar();
        buffer[i] = character;
        ++i;
    }
    while (character != '\n');

    buffer[i - 1] = '\0';
}

// Function to count determine the length of a string
int string_Length (char string[])
{
    int len = 0;

    while (string[len] != '\0')
    {
        ++len;
    }

    return len;
}

// Function to extract substring
void sub_String (char source[], int start, int count, char result[])
{
    int i, j, k;

    k = start + count;

    // Source[i] == '\0' is used in case count exceeds source string length
    for (i = start, j = 0; i < k || source[i] == '\0'; ++i, ++j)
    {
        result[j] = source[i];
    }

    result[j] = '\0';

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


推荐阅读