如下程序功能是统计文本文件“str.txt”中字符个数,请在______填上适当内容完成本程序。 int main(void ){ FILE * fp=fopen(str,"r") char ch=fgetc(fp); int n=0; while(ch!=_________){ n++; ch=fgetc(fp); } printf("%d\n",n); fclose(fp); return 0; }
第1题
[说明] 从键盘输入一个字符ch,输出该字符在文本文件input.txt 的每一行中出现的次数。(必须调用函数鳋统计ch的出现次数,函数ff (str,ch)的功能是统计并返回字符ch在字符串str 中出现的次数。)。
例如:如果文件input. txt 中存放了下列数据:
every
121
I am a student
运行程序,并输入e后,输出:
2
0
1
int ff( char * str, char ch)
{ int count =0;
while ((1)) {
if( *str= =ch) count++;
str ++;
}
return count;
}
include < stdio. h >
include < stdlib. h >
void main( )
{ char ch, c, s [80];
int k;
FILE *fp;
if((2)){
printf( “打不开文件!n”); return;
}
ch = getchar( );
k=0;
while( ! feof(fp) ) {
c = fgete(fp);
if (3)
s[k++ ] =c;
else {
s[k]= ";
printf ( "%dn" ,ff(s, ch) );
k=0;
}
}
(4)
printf( "% dn", ff( s, ch ) );
}
第2题
例如,输入“how do you do?”,则结果输出“ow do you do?h”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在 main函数的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio. h>
define N 80
main()
{
char str[N], ch;
int i;
clrscr ();
printf("\n Input a string:In");
gets (str);
printf("\n*** original string ***In");
puts (str);
ch=str [0];
for (i=0;【 】; i++)
str [i]=str [i+1];
【 】;
printf("\n *** new string ***\n");
puts (str);
}
第3题
第4题
注意:部分源程序给出如下。
请勿改动主函数main 和其他函数中的任何内容,仅在 main 函数的横线上填入所编写的若干表达式或语句。
试题程序:
include "stdio. h"
include "conio.h"
define N 80
main ()
{
FILE *fp;
int i=0;
char ch;
char str[N]="I'm a student!";
clrscr();
if ( (fp=fopen (【 】) ) ==NULL)
{
printf("cannot open out99. dat\n");
exit(0);
}
while (str[i])
{
ch=str[i];
【 】;
putchar(ch);
i++;
}
【 】;
}
第5题
例如,如果输入“cdefgh”,则输出为“hgfedc”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
define N 80
void fun (char s [], int n)
{
int i, j;
char ch;
for (i=0; i<n; i++)
for(j=【 】;j<n;j++)
if (s[i]<s [j])
{
ch=s [j];
【 】;
s [i] =ch;
}
main ( )
{
int i=0, strlen=0;
char str [N];
clrscr ();
printf ("\nInput a string: \n");
gets (str);
while (str [i] !=' \0')
{
strlen++;
i++;
}
fun (str, strlen);
printf ("\n***display string ***\n");
puts (str);
}
第6题
第7题
第8题
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<conio.h>
define LEN 80
main()
{
char str1[LEN],str2 [LEN];
char *p1=str1,*p2=str2;
int i=0,j=0;
clrscr();
printf("Enter the string:\n");
scanf(【 】);
printf("***the origial string***\n");
while(*(p1+j))
{
printf("【 】",*(p1+j));
j++;
}
for(i=0;i<j;i+=2)
*p2++=*(str1+i);
*p2='\0';
printf("\nThe new string is:%s\n", 【 】 );
}
第9题
第10题
例如:str1=“How do”,str2=“you do?”,结果输出: How do you do?
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<conio.h>
define N 40
void fun(char*str1,char *str2)
{
int i=0;
char *pl=str1;
char*p2=str2;
while(【 】)
i++;
for(;【 】;i++)
*(p1+i)=【 】;
*(p1+i)='\0';
}
main()
{
char str1[N],str2[N);
clrscr();
printf("*****Input the string str1 & Str2*****\n");
printf("\nstr1:");
gets(str1);
printf("\nstr2:");
gets(str2);
printf("**The string str1 & str2**\n");
puts(str1);
puts(str2);
fun(str1,str2);
printf("*****The new string *****\n");
puts(str1);
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!