如何编写一个C函数来保持读取一组行并标记行的长度

如何编写一个C函数来保持读取一组行并标记行的长度,c,function,C,Function,我在K&R中尝试一个练习,它要求编写一个程序,它读取一组文本行并打印最长的行 我是这样写的 define a string of characters write a function that marks the length of line until there's a newline when encountered newline, the function again repeats keep copying length to a variable if the length of

我在K&R中尝试一个练习,它要求编写一个程序,它读取一组文本行并打印最长的行

我是这样写的

define a string of characters
write a function that marks the length of line until there's a newline
when encountered newline, the function again repeats
keep copying length to a variable if the length of newline is greater
copy the string (if of greater length) to another string
print the last copied string
我试着写这个函数的长度,这肯定是错误的

int getlength(char str[],int lim)
{
    int length ,c;
    c = getchar();
    while((c=getchar())!= '\n')
    {
        for (length=0; length < lim -1; length++)
            c = str[length];
    }
    return length;
}
intgetLength(字符str[],intlim)
{
int长度,c;
c=getchar();
而((c=getchar())!='\n')
{
对于(长度=0;长度
有谁能建议我如何编写getlength函数吗。我不是要代码,而是psuedocode会非常有用。

使用strlen()可以获得字符串的长度。(我想这就是你要问的)

使用strlen()可以获得字符串的长度。(我想这就是你要问的)


好的,我明白了。要打印文本文件中最长的行。这是你的密码

#include<stdio.h>
#include<stdlib.h>
#define LINEMAX 200
int main(){
    FILE * fp = fopen("longestline.in","r");
    char temp1[LINEMAX]={'\0'},temp2[LINEMAX]={'\0'},*selected,*longest,c;
    int longestLength=0,length=0;
    selected = temp1;
    while((c=fgetc(fp))!=EOF){
        while(c!='\n'&&c!=EOF){
            selected[length] =c;
            length++;
            c=fgetc(fp);
        }

        if(length>longestLength){
            longestLength = length;
            selected[length+1] = '\0';
            longest = selected;
            selected = selected == temp1 ?temp2:temp1;
        }
        length =0;

    }
    puts(longest);
return 0;
}
#包括
#包括
#定义LINEMAX 200
int main(){
文件*fp=fopen(“longestline.in”,“r”);
字符temp1[LINEMAX]={'\0'},temp2[LINEMAX]={'\0'},*选中,*最长,c;
int longestLength=0,length=0;
所选=temp1;
而((c=fgetc(fp))!=EOF){
而(c!='\n'&&c!=EOF){
选择的[长度]=c;
长度++;
c=fgetc(fp);
}
如果(长度>最长长度){
最长长度=长度;
选定的[长度+1]='\0';
最长=已选择;
已选择=已选择==temp1?temp2:temp1;
}
长度=0;
}
推杆(最长);
返回0;
}

好的,我明白了。要打印文本文件中最长的行。这是你的密码

#include<stdio.h>
#include<stdlib.h>
#define LINEMAX 200
int main(){
    FILE * fp = fopen("longestline.in","r");
    char temp1[LINEMAX]={'\0'},temp2[LINEMAX]={'\0'},*selected,*longest,c;
    int longestLength=0,length=0;
    selected = temp1;
    while((c=fgetc(fp))!=EOF){
        while(c!='\n'&&c!=EOF){
            selected[length] =c;
            length++;
            c=fgetc(fp);
        }

        if(length>longestLength){
            longestLength = length;
            selected[length+1] = '\0';
            longest = selected;
            selected = selected == temp1 ?temp2:temp1;
        }
        length =0;

    }
    puts(longest);
return 0;
}
#包括
#包括
#定义LINEMAX 200
int main(){
文件*fp=fopen(“longestline.in”,“r”);
字符temp1[LINEMAX]={'\0'},temp2[LINEMAX]={'\0'},*选中,*最长,c;
int longestLength=0,length=0;
所选=temp1;
而((c=fgetc(fp))!=EOF){
而(c!='\n'&&c!=EOF){
选择的[长度]=c;
长度++;
c=fgetc(fp);
}
如果(长度>最长长度){
最长长度=长度;
选定的[长度+1]='\0';
最长=已选择;
已选择=已选择==temp1?temp2:temp1;
}
长度=0;
}
推杆(最长);
返回0;
}

以下是我认为您在阅读本文和您的评论时所寻找的内容:

int getlength(char str[],int lim)
{
    int length = 0;
    int c;
    while((c=getchar())!= '\n') //Get a character. If it's a newline, quit. Otherwise, keep going.
    {
        str[length] = c; //Add it to the string
        length++; //Move to the next character
    }
    str[length] = '\0'; //Add the terminating null to the end of the string
    return length;
}
此函数将查找您在stdin上给出的第一行的长度

以下是您原来的功能不起作用的原因:

你有:

int getlength(char str[],int lim)
{
    int length ,c;
    c = getchar();
    while((c=getchar())!= '\n')
    {
        for (length=0; length < lim -1; length++)
            c = str[length];
    }
    return length;
}
intgetLength(字符str[],intlim)
{
int长度,c;
c=getchar();
而((c=getchar())!='\n')
{
对于(长度=0;长度
首先,使用
c=getchar()读取的字符永远不会被处理,因为while语句在检查其条件时会覆盖它。记住,while循环将在每个循环开始时检查条件。我认为您想使用
do{…}while(条件),但如果这样做,您将遇到错误,因为如果用户不键入任何内容,则第一个getchar()调用将返回“\n”,循环仍将运行并将其添加到字符串中

第二,内部循环在您传递的字符串中的每个字符上循环,并将其设置为最近读取的字符。我用100个字符的字符串尝试了你的代码,输入了“abcdef”。我得到了99个f,它在“abcdef”中的每个字符之间循环,并将整个字符串设置为当前字符,因为“f”是最后一个字符,所以它被放在最后一个


第三,您没有在字符串末尾添加终止null。

以下是我认为您在阅读文章和您的评论时要寻找的内容:

int getlength(char str[],int lim)
{
    int length = 0;
    int c;
    while((c=getchar())!= '\n') //Get a character. If it's a newline, quit. Otherwise, keep going.
    {
        str[length] = c; //Add it to the string
        length++; //Move to the next character
    }
    str[length] = '\0'; //Add the terminating null to the end of the string
    return length;
}
此函数将查找您在stdin上给出的第一行的长度

以下是您原来的功能不起作用的原因:

你有:

int getlength(char str[],int lim)
{
    int length ,c;
    c = getchar();
    while((c=getchar())!= '\n')
    {
        for (length=0; length < lim -1; length++)
            c = str[length];
    }
    return length;
}
intgetLength(字符str[],intlim)
{
int长度,c;
c=getchar();
而((c=getchar())!='\n')
{
对于(长度=0;长度
首先,使用
c=getchar()读取的字符永远不会被处理,因为while语句在检查其条件时会覆盖它。记住,while循环将在每个循环开始时检查条件。我认为您想使用
do{…}while(条件),但如果这样做,您将遇到错误,因为如果用户不键入任何内容,则第一个getchar()调用将返回“\n”,循环仍将运行并将其添加到字符串中

第二,内部循环在您传递的字符串中的每个字符上循环,并将其设置为最近读取的字符。我用100个字符的字符串尝试了你的代码,输入了“abcdef”。我得到了99个f,它在“abcdef”中的每个字符之间循环,并将整个字符串设置为当前字符,因为“f”是最后一个字符,所以它被放在最后一个


第三,您没有在字符串末尾添加终止null。

我不明白您所说的“标记长度”是什么意思,或者您的函数应该做什么。您所说的“其字符”是什么意思?您所说的“输入字符串”是什么意思?我不知道您在说什么。如果你想计算字符串的长度,
strlen
会这样做,但我不知道这与你的代码或你的上一条评论有什么关系。@Anonymouse yes strlen()是我一直在寻找的东西,但我的问题是如何自己编写代码来计算一行的长度(这是字符串的长度,直到新行出现为止)我不明白你说的“标记长度”是什么意思,或者你的函数应该做什么。你说的“它的字符”是什么意思?你说的“输入字符串”是什么意思?我不知道你在说什么。如果