微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从C中源字符串的开头提取子字符串时出错

如何解决从C中源字符串的开头提取子字符串时出错

这来自C 4th Edition编程第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[],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。

解决方法

作为刚开始使用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[],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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。