Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A program that removes a certain character from a string of words.*/
- #include <stdio.h>
- #include <string.h>
- #define MAX 20 //array size
- int main()
- {
- char a[MAX]; //primary array
- char rmv; //removal character
- FILE* outfile;
- char* mode = "w"; //w for write/create mode
- char outfileName[] = "assign11.out";
- void rmchr ( char *a, char rmv); //function to remove characters
- printf("Please input a string and character to remove: ");
- scanf("%s %c", a, &rmv);
- printf("\nYour string before removal: %s\n", a);
- rmchr(a, rmv);
- printf("Your string after removal: %s", a);d
- outfile = fopen(outfileName, mode);
- if(outfile == NULL)
- {
- printf("Error, please try again.\n");
- }
- fprintf(outfile, "%s\n", a);
- fclose(outfile);
- return 0;
- }
- void rmchr ( char *a, char rmv) //function to remove characters
- {
- char temp[MAX]; //temp array
- char *ptr1= a; //primary pointer
- char *ptr2= temp; //temp pointer
- while (*ptr1 != '\0')
- if (*ptr1 != rmv)
- {
- *ptr2 = *ptr1;
- ptr1++;
- ptr2++;
- }
- else
- ptr1++;
- if (*ptr1 == '\0')
- *ptr2 = *ptr1;
- if (*ptr2 == '\0')
- strcpy(a,temp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement