This blog is under construction

Wednesday 17 July 2013

C program to remove punctuations in a string

Write a C program to remove punctuation in a string.


  #include <stdio.h>
  #include <string.h>

  int main() {
        char punctuation[] = { '.', '?', '!', ':', ';',
                                        '-', '(', ')', '[', ']',
                                        ',', '"', '/'};

        char input[256], output[256];
        int i, j, k, ch, flag;

        i = j = k = flag = 0;

        /* get the input string from the user */
        printf("Enter your input string:");
        fgets(input, 256, stdin);
        input[strlen(input) - 1] = '\0';

        /* copy characters other than punctuations */
        while (input[i] != '\0') {

                flag = 0;
                ch = input[i];

                for (j = 0; j < 13; j++) {
                        if (ch == punctuation[j]) {
                                flag = 1;
                                break;
                        }
                }

                if (!flag) {
                        output[k++] = input[i];
                }

                i++;
        }

        output[k] = '\0';

        /* print the resultant string */
        printf("Resultant String: %s\n", output);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input string:hai... Hello, how are you? I am Fine!!!:)))
  Resultant String: hai Hello how are you I am Fine



No comments:

Post a Comment