求助,程序运行出错了,问题如下
代码输入英文和数字出现中文乱码
输入中文直接错误
代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <locale.h>
#define MAX_SIZE 10000
// 定义线性表结构
typedef struct {
char data[MAX_SIZE];
int length;
}String;
void LIstInitiate(String* S)
{
S->length = 0;
}
void number(String* S)
{
int i;
for (i = 0; i < MAX_SIZE; i++)
{
if (S->data[i] != '\0')
S->length++;
else
break;
}
}
void showstring(String* S)
{
if (S->length == 0) printf("此系统中暂无信息");
else {
printf("输出字符数组:");
for (int i = 0; i < S->length; i++)
printf("%c", S->data[i]);
}
printf("\n");
}
void statistics(String* S)
{
char nem[10], chin[10], english[10]; int a = 0, b = 0, c = 0, d = 0, i = 0;
for (i = 0; i < S->length; i++)
{
if (isdigit(S->data[i]) != 0) {//运用了头文件ctype.h;
nem[a] = S->data[i];//当字符是数字时函数值不为0
a++;
}
else if (islower(S->data[i]) != 0 || isupper(S->data[i]) != 0)
{//两函数分别当字符为大写和小写英文时不为0
english[b] = S->data[i];
b++;
}
else if (isspace(S->data[i]) != 0)
{
c++;//对空格进行记数
}
else {
chin[d] = S->data[i];//剩下是中文
d++;
}
}
printf("数字:");
for (i = 0; i < a; i++)
printf("%c", nem[i]);
printf("英语:");
for (i = 0; i < b; i++)
printf("%c", english[i]);
printf("汉语:");
printf("%s", chin);
printf("空格数:%d", c);
printf("总个数:%d", a + b + d);
}
int count_word(String* S,char* word) {
int len_S = strlen(S->data);
int len_word = strlen(word);
int count = 0;
int i, j;
for (i = 0; i <= len_S - len_word; i++) {
for (j = 0; j < len_word; j++) {
if (S->data[i + j] != word[j]) {
break;
}
}
if (j == len_word) {
count++;
}
}
return count;
}
void replace(String* S,char* str2,char* str3) {
int i, j, k, done, count = 0, m = 0;
char temp[MAX_SIZE];
printf("输入一个指定字符串:\n");
scanf("%s", str3);
printf("输入一个要替换的字符串:\n");
scanf("%s", str2);
for (i = 0; i < strlen(S->data); i = i + m) {
if (S->data[i] == str2[0])
done = 0;
for (j = i, k = 0; k < strlen(str2); j++, k++) {
if (S->data[j] != str2[k]) {
done = 1;
m = k;
break;
}
}
if (done == 0) {
for (j = i + strlen(str2), k = 0; j < strlen(S->data); k++, j++)
{
temp[k] = S->data[j];
}
temp[k] = '\0';
for (j = i, k = 0; k < strlen(str3); j++, k++) { // 字符串替换
S->data[j] = str3[k];
count++;
}
for (k = 0; k < strlen(temp); j++, k++) { // 剩余字符串回接
S->data[j] = temp[k];
}
S->data[j] = '\0'; // 将字符数组变成字符串
m = strlen(str2);
}
else {
m = 1;
}
}
if (count == 0) {
printf("不能找到要替换的字符串!\n");
}
return;
}
int main() {
String string;
char word[MAX_SIZE];
char oldstring[MAX_SIZE];
char newstring[MAX_SIZE];
LIstInitiate(&string);
printf("请输入一段文字:\n");
gets_s(string.data);
number(&string);
showstring(&string);
printf("\n统计结果:\n");
statistics(&string);
printf("\n输入一个不含空格的单词:\n");
scanf("%s", word);
int count = count_word(&string, word);
printf("该单词出现次数为%d次:\n",count);
replace(&string, oldstring, newstring);
printf("替换后的字符串:%s\n", string);
return 0;
}