Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. /* A program that removes a certain character from a string of words.*/
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #define MAX 20 //array size
  7.  
  8. int main()
  9. {
  10. char a[MAX]; //primary array
  11. char rmv; //removal character
  12.  
  13. FILE* outfile;
  14. char* mode = "w"; //w for write/create mode
  15. char outfileName[] = "assign11.out";
  16.  
  17.  
  18. void rmchr ( char *a, char rmv); //function to remove characters
  19.  
  20. printf("Please input a string and character to remove: ");
  21. scanf("%s %c", a, &rmv);
  22. printf("\nYour string before removal: %s\n", a);
  23. rmchr(a, rmv);
  24. printf("Your string after removal: %s", a);d
  25.  
  26.  
  27. outfile = fopen(outfileName, mode);
  28. if(outfile == NULL)
  29. {
  30. printf("Error, please try again.\n");
  31. }
  32.  
  33. fprintf(outfile, "%s\n", a);
  34. fclose(outfile);
  35. return 0;
  36. }
  37.  
  38. void rmchr ( char *a, char rmv) //function to remove characters
  39. {
  40. char temp[MAX]; //temp array
  41.  
  42. char *ptr1= a; //primary pointer
  43. char *ptr2= temp; //temp pointer
  44. while (*ptr1 != '\0')
  45. if (*ptr1 != rmv)
  46. {
  47. *ptr2 = *ptr1;
  48. ptr1++;
  49. ptr2++;
  50. }
  51. else
  52. ptr1++;
  53. if (*ptr1 == '\0')
  54. *ptr2 = *ptr1;
  55. if (*ptr2 == '\0')
  56. strcpy(a,temp);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement