String problem

there is a string "1234567890"
I want to sum up the 1st , 3rd, 5th ,7th and 9th digits
how can I do it?
1- Parse the string characters using operator[], so if char* str = "35243", str[0] = 3, str[4]=4, and copy them to some other string.

2- use atoi to convert to integer

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

3- do all the summing you want.
Hope This Would Help. :)

#include<iostream.h>

int str[] = {1,2,3,4,5,6,7,8,9,0};
int i,sum=0,_sum=0,*x;

int main()
{

x = str;

for(i=0;i<10;i++)
{
cout << " " << *x;
if(*x%2==1)
sum=sum+ *x;
else
_sum=_sum+ *x;
x++;
}

cout << "\n\n SUM for Odd numbers:" << sum;
cout << "\n\n SUM for Even numbers:" << _sum;

return 0;

}
Last edited on
Topic archived. No new replies allowed.