文章编辑
功能:输入一页文字,程序可以统计出文字、数字、空格的个数。
静态存储一页文章,每行最多不超过80个字
符,共N行;要求
(分别统计出其中英文字母数和空格数及整篇文章总字数;
0 统计某一字符串在文章中出现的次数,并输出该次数;
( 删除某一子串,并将后面的宇符前移。
存储结构使用线性表,分别用几个子函数实现
相应的功能;
输入数据的形式和范国:可以输入大写、小写的英文字母、任何数字及标点符号。
输出形式:
(分行输出用户输入的各行字符;
12) 分4行输出"全部字母数"、"数字个数"、"空格个数"、"文章总字数"
6)输出删除某一字符串后的文章;
#include <stdio.h>
#include <string.h>
#define MAX_ROW 1000 // 最多行数
#define MAX_COL 80 // 每行最多字符数
// 统计文章中字母、数字、空格的个数
void count(char article[][MAX_COL+1], int rows, int *alphas, int *digits, int *spaces, int *total) {
*alphas = *digits = *spaces = *total = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < strlen(article[i]); j++) {
char c = article[i][j];
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
(*alphas)++;
} else if (c >= '0' && c <= '9') {
(*digits)++;
} else if (c == ' ') {
(*spaces)++;
}
(*total)++;
}
}
}
// 计算子串在文章中出现的次数
int countSubstring(char article[][MAX_COL+1], int rows, char *substr) {
int count = 0;
for (int i = 0; i < rows; i++) {
char *p = article[i];
while ((p = strstr(p, substr)) != NULL) {
count++;
p++;
}
}
return count;
}
// 删除文章中的子串,并将后面的字符前移
void deleteSubstring(char article[][MAX_COL+1], int rows, char *substr) {
for (int i = 0; i < rows; i++) {
char *p = article[i];
while ((p = strstr(p, substr)) != NULL) {
memmove(p, p+strlen(substr), strlen(p+strlen(substr))+1);
}
}
}
int main() {
char article[MAX_ROW][MAX_COL+1];
int rows = 0; // 行数
// 输入文章
printf("请输入文章,每行不超过80个字符,以空行结束:\n");
while (rows < MAX_ROW) {
if (fgets(article[rows], MAX_COL+1, stdin) == NULL || strcmp(article[rows], "\n") == 0) {
break;
}
rows++;
}
// 分别输出各行字符
printf("您输入的文章是:\n");
for (int i = 0; i < rows; i++) {
printf("%s", article[i]);
}
// 统计字母、数字、空格的个数以及文章总字数
int alphas, digits, spaces, total;
count(article, rows, &alphas, &digits, &spaces, &total);
printf("全部字母数:%d\n", alphas);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("文章总字数:%d\n", total);
// 统计子串在文章中出现的次数
char substr[MAX_COL+1];
printf("请输入要搜索的字符串:");
fgets(substr, MAX_COL+1, stdin);
substr[strcspn(substr, "\n")] = '\0'; // 去掉换行符
int substr_count = countSubstring(article, rows, substr);
printf("'%s'在文章中出现了%d次。\n", substr, substr_count);
// 删除子串并输出文章
printf("请输入要删除的字符串:");
fgets(substr, MAX_COL+1, stdin);
substr[strcspn(substr, "\n")] = '\0'; // 去掉换行符
deleteSubstring(article, rows, substr);
printf("删除'%s'后的文章是:\n", substr);
for (int i = 0; i < rows; i++) {
printf("%s", article[i]);
}
return 0;
}
#include <stdio.h>
void main(){
int i,j,q=0,p=0,z=0,o=0,n=0;
char a[3][80];
for(i=0;i<3;i++){
gets(a[i]);
}
for(i=0;i<3;i++){
for(j=0;a[i][j]!='\0';j++){
if(a[i][j]>'A' && a[i][j]<'Z'){q++;}
if(a[i][j]>'a' && a[i][j]<'z'){p++;}
if(a[i][j]>'0' && a[i][j]<'9'){z++;}
if(a[i][j]==' '){o++;}
else{n++;}
}
}
printf("大写字母:%d\n小写字母:%d\n数字:%d\n空格:%d\n其它:%d\n",q,p,z,o,n);
}