Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从生产线末端移除坯料_C_String_Console Application - Fatal编程技术网

C 从生产线末端移除坯料

C 从生产线末端移除坯料,c,string,console-application,C,String,Console Application,我正在尝试打印行,在开始和结束时没有空格。 我不明白为什么从末端移除不起作用 #include <stdio.h> #define MAX_LINE_LENGTH 1000 #define LINE_BEGIN 0 #define LINE_MIDDLE 1 #define INBLANK 2 void deleteBlankFromEnd(char line[], int offset); void deleteLine(char line[], int offset);

我正在尝试打印行,在开始和结束时没有空格。 我不明白为什么从末端移除不起作用

#include <stdio.h>

#define MAX_LINE_LENGTH 1000

#define LINE_BEGIN 0
#define LINE_MIDDLE 1
#define INBLANK 2


void deleteBlankFromEnd(char line[], int offset);
void deleteLine(char line[], int offset);

main()
{
    int c, i, status ;
    i = status = 0;
    char line[MAX_LINE_LENGTH];
    while((c = getchar()) != EOF) { 
        if (c == ' ' || c == '\t') {
            if (status == LINE_MIDDLE || status == INBLANK) {
                line[i++] = c;
                if (status == LINE_MIDDLE)
                    status = INBLANK;
            }
        } else if (c == '\n') {
            if (status > 0) {
                if (status == INBLANK) {
                    printf("Line length = %d ", i);
                    deleteBlankFromEnd(line, i); 
                }
                printf("%s", line);
                printf("End\n");

                deleteLine(line, i);
            }
            i = 0;
            status = LINE_BEGIN;
        } else {
            line[i++] = c;
            status = LINE_MIDDLE;
        }
    }
}

void deleteBlankFromEnd(char line[], int offset) {
    while (line[offset] == ' ' || line[offset] == '\t') {
        line[offset--] = 0;
    }
    printf("Line length = %d ", offset);
}

void deleteLine(char line[], int offset) {
    while (offset >= 0) {
        line[offset--] = 0;
    }
}
#包括
#定义最大线长度1000
#从0开始定义行
#定义线_中间1
#定义INBLANK 2
void deleteBankFromEnd(字符行[],int偏移量);
void deleteLine(字符行[],int偏移量);
main()
{
INTC,i,状态;
i=状态=0;
字符行[最大行长度];
而((c=getchar())!=EOF){
如果(c=''| | c='\t'){
如果(状态==行| | |状态==行内){
行[i++]=c;
如果(状态==行中)
状态=内部空白;
}
}else如果(c=='\n'){
如果(状态>0){
如果(状态==INBLANK){
printf(“行长度=%d”,i);
deleteBlankFromEnd(第i行);
}
printf(“%s”,第行);
printf(“End\n”);
删除行(行,i);
}
i=0;
状态=行\开始;
}否则{
行[i++]=c;
状态=中间线;
}
}
}
void deleteBankFromEnd(字符行[],整数偏移){
而(行[偏移]=''| |行[偏移]='\t'){
行[偏移量--]=0;
}
printf(“行长度=%d”,偏移量);
}
void deleteLine(字符行[],整数偏移){
而(偏移量>=0){
行[偏移量--]=0;
}
}

在我看来,索引错误是一个一个接一个的错误。如果初始偏移处的字符不是空格或制表符,则deleteBlankFromEnd将不会执行任何操作;试着找出它是什么?您可能需要从--i

开始传递给
deletebankfromend
函数,该函数的偏移量在您的情况下等于输入的长度。通过这种方式,您可以尝试访问实际上超出范围的内容:

while (line[offset] == ' ' || line[offset] == '\t')
您最好按如下方式调用deleteBlankFromEnd:

deleteBlankFromEnd(line, i-1);
其中第二个参数将指向字符串中的最后一个字符