Tuesday 24 June 2014

C++ program to print the characters at even and odd positions of a string


Here is a C++ program to print the characters at even and odd positions of a string


 SOURCE CODE OUTPUT
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[30];
    int i,lenght;
    cout<<"Enter String : ";
    cin>>str;
    cout<<"Characters at odd places : ";
    lenght=strlen(str)-1;
    for(i=0;i<=lenght;i+=2)
        cout<<str[i]<<" ";
    cout<<endl<<"Characters at even places : ";
    for(i=1;i<=lenght;i+=2)
        cout<<str[i]<<" ";
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment