Click here to Skip to main content
16,016,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have a input string eg "123456789098765432" need to split into two arrays OddArray and EvenArray based on the positionsi.e, all the digits in even position(2,4,6,8....) to EvenArray and all the digits in oddposition(3,5,7...) to OddArray

What I have tried:

C#
char EvenArray[256],OddArray[256],Inputarray[256];
CString dis = "123456789098765432";
strcpy(Inputarray,dis);
int i,j,k;
int x = strlen(dis);
for(i=0;i<x;i++)>
	{
		if((i+2)%2 ==0)
		{
			
			EvenArray[j]=Inputarray[i];
			j++;
		}
		else
		{
			OddArray[k]=Inputarray[i];
			k++;
		}
	}
Posted
Updated 23-Jun-16 16:48pm
v3
Comments
jeron1 23-Jun-16 10:13am    
Have I completely missed something? You are doing nothing with the CString that supposedly contains the data to be processed, and CString has its own GetLength() method.
Member 11648410 23-Jun-16 22:49pm    
strcpy(Inputarray,dis); forgot to add this ..Sorry
jeron1 24-Jun-16 10:31am    
Can't you access elements of the CString the same way, with the [] operator? You could get rid of InputArray[], and strcpy(), the variable x, and alter the for loop to look like for(i = 0; i< dis.GetLength(); i++)
phil.o 24-Jun-16 0:23am    
For oddity test, I suggest to use if ((i & 1) == 0) instead.

1 solution

Well... that needs a little tweaking.
You aren't processing it correctly - your definition of "odd" and "even" excludes zero and unity.
Zero I can understand (it's neither odd nor even) but one is odd.
But to exclude them, it's simple: change the initial value of i:
C++
for(i=2;i<x;i++)>
Should do it.

(Do bear in mind that arrays in C / C++ / C# all start with element at index zero, not index 1: so in the string "12345" the character at index 2 is "3".
If your position values don't allow for that, you need to tweak your code slightly.
 
Share this answer
 
Comments
CPallini 23-Jun-16 8:28am    
0 is even.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900