[英]Replacing newlines with spaces and sorting words in a specific lines length
我刚刚打开了一个文本文件,其中包含几行不同长度的行,有些行以“。”开头。 (点),因此将它们定义为函数,稍后再处理。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char line[10000];
char filename[30];
FILE *fp;
int i;
int u;
printf("Enter file name with extension\n");
gets(filename);
fp = fopen(filename,"r");
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
我努力尝试了两天,以便在同一行中打印其余的行(不是函数),但是当您使用strtok
函数时,如您所见,它删除了它而没有添加空格,因此来自不同行的字符混合在一起了。
第二个问题是我试图将行长度限制为最多60个字符,并且如果长度等于或小于该长度,则将单词适合空格。
while(fgets(line,10000,fp) != NULL )
{
if(line[0]!='.'){
strtok(line,"\n");
for(u=0;u<strlen(line);u++){
if(u==60){
printf("\n");
}
else{printf("%c",line[u]);}
}
printf("%s",line);
}
else{printf("");}
}
fclose(fp);
return 0;
}
您需要一种类似于以下内容的算法:
strtok()
也许是strtok()
或strcspn()
或strpbrk()
) 这使您忽略了功能线。 或者,您可以刷新当前输出行(如果不为空),打印功能行,然后转到下一行。
请注意,如果下一个单词是60个字符或更长,则不会截断或拆分它; 它只会自己占据一条线。 注意,缓冲区没有溢出是您的责任。 例如,您可以刷新上一个输出行,打印该单词及其换行符,然后将输出重置为空。 或者,您可以拆分单词(但是您会拆分其中的一部分以适合当前行,其余拆分为下一行,还是只是将起始点强制到下一行然后拆分?)。 如果单词长度为250个字符怎么办?
这是上述算法的相当直接的音译。 它从标准输入读取; 我拒绝回答有关打开哪个文件名的提示。 如果我要处理任意文件,它们将在命令行中作为参数传递。
#include <stdio.h>
#include <string.h>
/*
.function line should not appear in output
*/
/*
WordOfMoreThanSixtyCharacters--WithNaryASpaceInSight--ThoughThereIsPunctuation!
*/
enum { OUT_LENGTH = 60 };
int main(void)
{
char oline[OUT_LENGTH]; // output line
char iline[4096]; // input line
int olength = 0; // length of data in output line
const char separators[] = " \n\t";
while (fgets(iline, sizeof(iline), stdin) != 0)
{
if (iline[0] == '.')
continue;
int wlength; // word length
char *wline = iline; // start of word
wline += strspn(wline, separators);
while ((wlength = strcspn(wline, separators)) > 0)
{
if (olength + 1 + wlength > OUT_LENGTH)
{
printf("%.*s\n", olength, oline);
olength = 0;
if (wlength >= OUT_LENGTH)
{
printf("%.*s\n", wlength, wline);
wline += wlength;
wlength = 0;
}
}
if (wlength > 0)
{
if (olength > 0)
oline[olength++] = ' ';
strncpy(&oline[olength], wline, wlength);
olength += wlength;
wline += wlength;
}
wline += strspn(wline, separators);
}
}
if (olength > 0)
printf("%.*s\n", olength, oline);
return 0;
}
在其自身的源代码上运行时的输出:
#include <stdio.h> #include <string.h> /* */ /*
WordOfMoreThanSixtyCharacters--WithNaryASpaceInSight--ThoughThereIsPunctuation!
*/ enum { OUT_LENGTH = 60 }; int main(void) { char
oline[OUT_LENGTH]; // output line char iline[4096]; // input
line int olength = 0; // length of data in output line const
char separators[] = " \n\t"; while (fgets(iline,
sizeof(iline), stdin) != 0) { if (iline[0] == '.') continue;
int wlength; // word length char *wline = iline; // start of
word wline += strspn(wline, separators); while ((wlength =
strcspn(wline, separators)) > 0) { if (olength + 1 + wlength
> OUT_LENGTH) { printf("%.*s\n", olength, oline); olength =
0; if (wlength >= OUT_LENGTH) { printf("%.*s\n", wlength,
wline); wline += wlength; wlength = 0; } } if (wlength > 0)
{ if (olength > 0) oline[olength++] = ' ';
strncpy(&oline[olength], wline, wlength); olength +=
wlength; wline += wlength; } wline += strspn(wline,
separators); } } if (olength > 0) printf("%.*s\n", olength,
oline); return 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.