!人民币奖励!C语言设计文章编辑。功能:输入一页文字,程序可以统计出文字,数字,空格的个数

功能:输入一页文字,程序可以统计出文字、数字、空格的个数。
  静态存储一页文章,每行最多不超过80个字符,共N行;要求(1)分别统计出其中英文字母数和空格数及整篇文章总字数;(2)统计某一字符串在文章中出现的次数,并输出该次数;(3)删除某一子串,并将后面的字符前移。
  存储结构使用线性表,分别用几个子函数实现相应的功能;
  输入数据的形式和范围:可以输入大写、小写的英文字母、任何数字及标点符号。
输出形式:(1)分行输出用户输入的各行字符;(2)分4行输出"全部字母数"、"数字个数"、"空格个数"、"文章总字数"(3)输出删除某一字符串后的文章;
!求大神!要求不能和网上有重复的(要是被老师发现就算零分了)要加注释解释这行是什么意思。支持网银支付宝Q币话费等一系列转账方式
用CodeBlocks实现


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

FILE *fp;    //输入文件

/*
** 函数功能:统计文件中的全部字母数、数字个数、空格个数、总字数
*/
void get_count( int *count )
{
char input[81];
int i ;

while( fgets( input, 80, fp ) != NULL ){
i = 0;
while( input[i] != '\0' ){
count[3]++;       //求总字数
if( isalpha( input[i] ) )
count[0]++;   //求字母数
else if( isdigit( input[i] ) )
count[1]++;   //求数字个数
else if( input[i] == ' ' )
count[2]++;   //求空格数
i++;
}
}

return;
}

/*
** 函数功能:求字符串str_appear在文章中出现的次数
*/
void get_count_str( char *str_appear, int *count )
{
char input[81];

if( ( fp = fopen( "input_data.txt", "r" ) ) == NULL ){ //打开文件
printf( "Can not open file input_data.txt!\n" );
exit( 1 );
}

while( fgets( input, 80, fp ) != NULL ){
if( strstr( input, str_appear ) != NULL )       //如果str_appear是input的字串,则返回非零指针
count[4]++;
}

return;
}

/*
** 函数功能:在原文章中删除某一子串后的内容
*/
void delete_str( char *str_delete, FILE *fp_out )
{
char input[81];
char *p;
int i, j, len_str_delete, len_p, len_input;
if( ( fp = fopen( "input_data.txt", "r" ) ) == NULL ){ //打开文件
printf( "Can not open file input_data.txt!\n" );
exit( 1 );
}

len_str_delete = strlen( str_delete );                //要删除子串的长度

while( fgets( input, 80, fp ) != NULL ){    
while( ( p = strstr( input, str_delete ) ) != NULL ){  //在input中删除子串str_delete
len_input =  strlen( input );
len_p = strlen(p);
for( i = 0; i < len_str_delete; i++ )
for( j = len_input - len_p; j < len_input; j++ )
input[j] = input[j + 1];
}
fprintf( fp_out, "%s", input );          //将删除了子串后的input保存到新的文件中
}

    fclose( fp_out );

return;
}

int main( void )
{
char input[81];
int count[5]={0};        //用来存储全部字母数、数字个数、空格个数、总字数以及某字符串出现的次数
char str_appear[20];     //用来统计在文章中出现次数的字符串
char str_delete[20];     //要在文章中删除的字符串
FILE *fp_out;            //用来存储删除了字符串后的文章

if( ( fp = fopen( "input_data.txt", "r" ) ) == NULL ){      //打开初始文件
printf( "Can not open file input_data.txt!\n" );
exit( 1 );
}

if( ( fp_out = fopen( "output_data.txt", "w" ) ) == NULL ){ //创建删除字串后的文件
printf( "Can not create file output_data.txt!\n" );
exit( 1 );
}
    
/**********  过程处理   ************/
    get_count( count );     //用来计算全部字母数、数字个数、空格个数、总字数

printf( "请输入用来统计在文章中出现次数的字符串:\n" );
    
gets(str_appear);

    get_count_str( str_appear, count ); //用来统计子串str_appear在文章中出现的次数
    
printf( "请输入要删除的字符串:\n" );

gets( str_delete );                

delete_str( str_delete, fp_out );  //在文章中删除字串str_delete
   
    
    /************处理后的结果输出*************/
printf( "\n\n" );
    printf( "文章中的字母个数为:%d\n", count[0] );
printf( "文章中的数字个数为:%d\n", count[1] );
printf( "文章中的空格个数为:%d\n", count[2] );
printf( "文章中的总个数为:%d\n"  , count[3] );
printf( "文章中的字串%s出现的次数为:%d\n", str_appear,count[4] );

printf( "\n\n\n原来的文章内容为:\n");
if( ( fp = fopen( "input_data.txt", "r" ) ) == NULL ){      //打开初始文件
printf( "Can not open file input_data.txt!\n" );
exit( 1 );
}
while( fgets( input, 80, fp ) != NULL )
printf("%s", input );


printf( "\n\n\n删除子串%s的文章内容为:\n", str_delete );
if( ( fp_out = fopen( "output_data.txt", "r" ) ) == NULL ){  //打开删除字串后的文件
printf( "Can not create file output_data.txt!\n" );
exit( 1 );
}
while( fgets( input, 80, fp_out ) != NULL )
printf("%s", input );

printf( "\n" );
    
return 0;
}

/*
** 程序运行之前,在相应的目录下创建文件input_data.txt,并输入一些内容。然后编译,运行程序。
** 然后程序会提示你输入用来统计在文章中出现次数的字符串以及输入要删除的字符串,最后程序输出结果。
*/

温馨提示:答案为网友推荐,仅供参考
相似回答