This blog is under construction

Tuesday 16 July 2013

C program to print alternate characters in a string

Write a C program to print alternate characters in a string.


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

  int main() {
        char data[256];
        int i = 0;

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

        printf("Characters at odd position:");

        /* print alternate characters in i/p string */
        while (data[i] != '\0') {

                /* printing characters at odd position */
                if (i % 2 == 0) {
                        printf("%c", data[i]);
                }

                i++;
        }

        printf("\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input string: Helloworld
  Characters at odd position: Hlool


No comments:

Post a Comment