Implement a C++ program to define function named as ext, to extract the digits that located at even and odd position of five digit number and show the result.
Runtime Input :
00156
Output :
016
05
#include <iostream>
#include <string>
using namespace std;
void ext(string s){
int len=s.length();
string sEven;
string sOdd;
for(int i=0;i<len;i++){
if ((i+1)%2==0){
sEven=sEven+s[i];
}
else{
sOdd=sOdd+s[i];
}
}
cout<<"\nDigits at odd positions are: "<<sOdd<<endl;
cout<<"\nDigits at even positions are: "<<sEven<<endl;
}
int main(){
string str1;
cout<<"Enter a five digit number: ";
cin>>str1;
ext(str1);
}
Comments
Leave a comment