一锅意大利面 懒得再写函数了
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void splitWord(char *p) {
int i = 0, LEN = strlen(p);
for(i = 0; i < LEN; i++) {
if((*(p + i) < 'A' || *(p + i) > 'z') || ((*(p + i) > 'Z' && *(p + i) < 'a'))) {
*(p + i) = '\n';
}
}
}
char *cutspace(char *p) {
int i = 0, j = 0, LEN = strlen(p);
for(i = 0; i < LEN; i++) {
if(*(p + i) != '\n') {
p += i;
break;
}
}
for(j = LEN - 1 - i; j > 0; j--) {
if(*(p + j) != '\n') {
*(p + j + 1) = '\0';
break;
}
}
return p;
}
int main(void) {
struct wordclac {
char word[80];
int len;
};
struct wordclac warr[80];
int i = 0, flag = 0, line = 0, lenmax = 0;
char c, *input, *pstr, *filename = "strdata.txt", *filetmp = "strtmp.txt";
FILE *fp, *ftp;
input = (char *)malloc(sizeof(char));
if(input != NULL) {
while((input[i] = getchar()) != '\n') {
input = (char *)realloc(input, (i + 2) * sizeof(char));
if(input != NULL)
i++;
else {
printf("ERROR!\n");
exit(1);
}
}
input[i] = '\0';
} else {
printf("ERROR!\n");
exit(1);
}
splitWord(input);
pstr = cutspace(input);
fp = fopen(filename, "w+");
fwrite(pstr, sizeof(char), strlen(pstr), fp);
rewind(fp);
ftp = fopen(filetmp, "w+");
while((c = fgetc(fp)) != EOF) {
if(c == '\n' && !flag) {
continue;
} else if(c == '\n' && flag) {
flag = 0;
fputc(c, ftp);
} else {
fputc(c, ftp);
flag = 1;
}
}
rewind(ftp);
while((c = fgetc(ftp)) != EOF) {
if(c == '\n') {
line++;
}
}
rewind(ftp);
for(i = 0; i <= line; i++) {
fscanf(ftp, "%[^\n]", warr[i].word);
fgetc(ftp);
warr[i].len = strlen(warr[i].word);
printf("%s\t", warr[i].word);
printf("%d\n", warr[i].len);
}
for(i = 0; i <= line; i++) {
if(warr[i].len > lenmax) {
lenmax = warr[i].len;
}
}
printf("最大字符串长度: %d\n", lenmax);
for(i = 0; i <= line; i++) {
if(warr[i].len == lenmax) {
printf("%s\n", warr[i].word);
}
}
fclose(fp);
fclose(ftp);
free(input);
return 0;
}
[
本帖最后由 longwu9t 于 2014-12-21 23:02 编辑 ]