Problem C: 文件---单词首字母大写
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 307 Solved: 139
[Submit][Status][Web Board]
Description
文本文件data.dic存有若干行由英文单词组成的句子,相邻单词之间有一个空格分割,前三行的信息为:
however business leaders have argued that
immigration boosts the amarican economy
and that ending the daca programme
编写程序,输入起始行和终止行,将该范围内的每个单词的首字母改为大写并将结果输出到屏幕上,注意文件中只有小写的英文单词,无其他成分。请完善如下程序:只需提交需要填写部分的代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
将每个单词的首字母大写,文件中只存在单词,而且均为小写
*/
int main()
{
FILE *fp;
char str[100];
int start, end, i, linecount = 0,slen;
/*i为迭代变量,linecount记录当前处理行数是第几行,默认从0开始计数*/
if( (fp = fopen("data.dic","r")) == NULL )
{
printf("can't read data.dic!\n");
exit(-1);
}
scanf("%d %d",&start,&end);
while( !feof(fp) )
{
fgets(str,100,fp);
linecount++;
if( linecount<start ) //判断是否是start至end行,不是则继续读文件
continue;
if( linecount>end )
break;
if(str[0]>'z'||str[0]<'a')
break;
slen = strlen(str);
for( i = 0 ; i < slen; i++) //符合条件
{
/*注意每一行开始的单词前没有空格*/
if( i == 0)
str[i] = str[i] - 32;
/*******在下面填写代码***********/
/*******在上面填写代码***********/
}
printf("%s",str); //输出处理后的字符串
}
fclose(fp);
return 0;
}
Input
输入两个数字start,end(start<end)
Output
输出从start行到end行的修改后的信息
Sample Input
1 2
Sample Output
However Business Leaders Have Argued That
Immigration Boosts The Amarican Economy
#include <stdio.h> #include <stdlib.h> #include <string.h> /* 将每个单词的首字母大写,文件中只存在单词,而且均为小写 */ int main() { FILE *fp; char str[100]; int start, end, i, linecount = 0,slen; /*i为迭代变量,linecount记录当前处理行数是第几行,默认从0开始计数*/ if( (fp = fopen("data.dic","r")) == NULL ) { printf("can't read data.dic!\n"); exit(-1); } scanf("%d %d",&start,&end); while( !feof(fp) ) { fgets(str,100,fp); linecount++; if( linecount<start ) //判断是否是start至end行,不是则继续读文件 continue; if( linecount>end ) break; if(str[0]>'z'||str[0]<'a') break; slen = strlen(str); for( i = 0 ; i < slen; i++) //符合条件 { /*注意每一行开始的单词前没有空格*/ if( i == 0) str[i] = str[i] - 32; else if(str[i-1]==' ') str[i]=str[i]-32; } printf("%s",str); //输出处理后的字符串 } fclose(fp); return 0; }