Sunday, April 15, 2012

Program 25TH in C++


PROBLEM STATEMENT :

If you are given an input array of chars 1 2 3 4 a b c d
output should be 1 a 2 b 3 c 4 d
No Buffers should be used
variables are permissible.

SOLUTION :

#include <iostream.h>
#include <cstdio>

int main()
{

char a1[8]={'1','2','3','4','a','b','c','d'};

int strlen=sizeof(a1)/sizeof(char);
int i=0;
int j=0;

for(i=0; i<strlen; i++)
{

if(a1[i]<48 || a1[i]>57)
{
j=i;
break;
}

}


for(i=1; j<strlen; i+=2)
{

int k=j++;
char temp = a1[k];
while(k > i)
{
a1[k]=a1[k-1];
k--;

}
a1[k]=temp;
}


cout<<"\n\no/p : ";
for(i=0; i<strlen; i++)
{
cout<<a[i]<<" ";
}

}


No comments:

Post a Comment