【发布时间】:2014-03-12 17:33:04
【问题描述】:
所以我正在接受这样的文本文件:
Hello. This is my test! I need to print sentences.
Can I do it? Any help is welcomed. Thanks!
我正在尝试输出:
1. Hello.
2. This is my test!
3. I need to print sentences.
4. Can I do it?
...and so on....
这是我目前所写的:
#include <stdio.h>
#include <stdlib.h>
main() {
int storage[50];
int i = 0 ;
int linecount = 1 ;
char c;
for (;;) {
c=getchar();
if(c == '\n'){
c == '\0';}
storage[i] = c;
i++;
if (c == '.' || c == '!' || c == '?') {
int j ;
printf("%d. ", linecount++);
for (j = 0; j < i; j++) {
printf("%c", storage[j]); }
i = 0 ;
printf("\n");
}
}
}
结果是输出:
1. Hello.
2. This is my test!
3. I need to print sentences.
4.
Can I do it?
5. Any help is welcomed.
6. Thanks!
我的第一个问题是它为第 4 行打印了一个 '\n',我认为我的代码:
if(c == '\n'){
c == '\0';}
会解决这个问题,但事实并非如此。
我的第二个问题是它在第一条记录之后添加了一个额外的空格字符。 我知道这是因为我使用了 print 语句:
"%d. "
而且句子的开头也有一个空格与最后一句分开,但我不确定如何解决这个问题。任何帮助都会很棒!谢谢! -qorz
【问题讨论】: