/*
用户从控制台输入一篇文章,输入完成以后,判读文章中一共有多少个单词?
char txt[2000];
hello lili,hello hameimei,nice to meet you.
hello nice to meet you too.
*/
#include <stdlib.h>
#include <string.h>
#define N 2000
void mystrcat(char * str1,char * str2)
{
while (*str1!='\0') {
str1++;
}
while (*str2!='\0') {
*str1=*str2;
str2++;
str1++;
}
*str1='\0';
}
int main(int argc, const char * argv[])
{
char txt[N];
//提示用户输入文章
printf("请输入你的文章:");
gets(txt);
//printf("%s\n",txt);
for (int j = 0; txt[j]!= '\0'; j++) {
putchar(txt[j]);
}
printf("\n");
int num = 0;//单词个数
int i = 0;
while (txt[i] != '\0')
{
if (((txt[i]>=48&&txt[i]<=57)||(txt[i]>=65&&txt[i]<=90)||(txt[i]>=97&&txt[i]<=122))&&((txt[i+1]<48)||(txt[i+1]>57&&txt[i+1]<65)||(txt[i+1]>90&&txt[i+1]<97)||(txt[i+1]>122)))
{
num++;
}
i++;
}
printf("文章单词个数为%d\n",num);
// 文件处理
FILE *fp;
char fileName[20];
printf("请输入文件名:");
scanf("%s",fileName);
char newname[] = {"//Users//haixia//Desktop//"};
mystrcat(newname, fileName);
//文件打开
fp = fopen("//Users//haixia//Desktop//新建文件.txt", "w");
rename("//Users//haixia//Desktop//新建文件.txt",newname);
//写入文件
fprintf(fp, txt);
//关闭文件
fclose(fp);
return 0;
}