masterm1nd99

палиндром

Jan 11th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAX 100
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. void writeToFile() {
  7. FILE *f = fopen("text.txt", "w");
  8. char c;
  9. while((c = getchar()) != '#') {
  10. fputc(c, f);
  11. }
  12. fclose(f);
  13. }
  14.  
  15. void printFile() {
  16. FILE *f=fopen("print.txt","r");
  17. char line[100];
  18. while(!feof(f)){
  19. fgets(line,100,f);
  20. if (feof(f))
  21. break;
  22. printf("%s",line);
  23. }
  24. fclose(f);
  25. }
  26.  
  27. int e_palindrom(char c[])
  28. {
  29. int i,j,k=0;
  30. char n[MAX];
  31. for (i=0;i<strlen(c);i++)
  32. {
  33. if (isalpha(c[i]))
  34. {
  35. n[k]=c[i];
  36. k++;
  37. }
  38. }
  39. n[k]='\0';
  40. for (i=0,j=strlen(n)-1;i<strlen(n);i++,j--)
  41. {
  42. if (tolower(n[i])!= tolower(n[j])) return 0;
  43. }
  44. for (i=0;i<strlen(n);i++)
  45. {
  46. c[i]=n[i];
  47. }
  48. c[i]='\0';
  49. return 1;
  50. }
  51.  
  52. int main() {
  53. writeToFile();
  54.  
  55. char n[MAX];
  56. FILE *dat1=fopen("text.txt","r");
  57. FILE *dat2=fopen("print.txt","w");
  58. while (fscanf(dat1,"%s",n)!=EOF)
  59. {
  60. if (e_palindrom(n))
  61. {
  62. fprintf(dat2,"%s\n",n);
  63. }
  64. }
  65. fclose (dat1);
  66. fclose (dat2);
  67. printFile();
  68. return 0;
  69. }
Add Comment
Please, Sign In to add comment