Hôm qua em vửa tìm 1 bài trên mạng yêu cầu truyền dữ liệu từ một tập tin này sang một tập tin khác, loại bỏ tất cả các nguyên âm (a, e, i, o, u). Loại bỏ các nguyên âm ở dạng chữ hoa lẫn chữ thường và hiển thị nội dung của tập tin mới.
Đoạn code của e, sai ở đâu mọi người chỉnh sửa giúp:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<fstream>
#include<stdlib.h>
int main()
{
FILE *fp1;
FILE *fp2;
char str1 [14], str2[14];
int temp,j;
int count;
if ((fp1 = fopen("phong1.txt", "r")) == NULL)
{
printf ("Cannot open file \n\n");
exit(1);
}
printf("read the string of phong1:\n");
while (fgets(str1,sizeof(str1),fp1)!=NULL)
printf("%s",str1);
int n = strlen(str1);
for(int i = 0;i<n;i++)
{
switch(str1[i])
{
case 'a':
case 'e':
case 'u':
case 'i':
case 'o':
case 'A':
case 'E':
case 'U':
case 'I':
case 'O':
count++;
}
for(int a=n;a<n+count;a++)
{
str1[a]=' ';
}
str1[n+count]=NULL;
for(j =0;j<count;j++)
{
temp=str1[i];
str1[i]=str1[n+j];
str1[n+j]=temp;
}
}
if ((fp2 = fopen("phong2.txt", "wb")) == NULL)
{
printf ("Cannot open file \n\n");
exit(1);
}
printf("\nenter the string of fp1 into fp2:\n");
fwrite(str1,sizeof(str1),1,fp2);
fclose(fp2);
if ((fp2 = fopen("phong2.txt", "rb")) == NULL)
{
printf ("Cannot open file \n\n");
exit(1);
}
while(fread(str2,sizeof(str2),1,fp2)==1)
{
printf("%s",str2);
}
fclose(fp2);
return(0);
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?