Swapping elements in the array recursively

I'm trying to swap elements in my array with the following conditions

1. Scan the array elements from left to right until a uppercase
2. Scan the array element from the right to left until a lowercase
3. Swap the two letters.
4 Move all the lowercase to the left and upper case to the left

Hence, i created a swapElement function to swap element recursively by checking every single possible condition hence it will be a little lengthy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  void swapElementR(char alphabets[], int size)
{

	int temp;
	int i = 0;
	int j = size - 1;
   	   
	
	if (i >= j)
		
		return;
		
	else if(alphabets[i] >= 'A' && alphabets[i] <= 'Z' && alphabets[j] >= 'a' && alphabets[j] <= 'z')
	{
		temp = alphabets[i];
		alphabets[i] = alphabets[j];
		alphabets[j] = temp;
		
		--j;
		++i;
		
		swapElementR(alphabets, size);	   
	}
	
	else if (alphabets[i] >= 'A' && alphabets[i] <= 'Z' && alphabets[j] >= 'A' && alphabets[j] <= 'Z')
	{
		--j;
		
		swapElementR(alphabets, size);
	}
	
	else if (alphabets[i] >= 'a' && alphabets[i] <= 'z' && alphabets[j] >= 'a' && alphabets[j] <= 'z')
	{
		++i;
		
		swapElementR(alphabets, size);
	}

	else if (alphabets[i] >= 'a' && alphabets[i] <= 'z' && alphabets[j] >= 'A' && alphabets [j] <= 'Z')
	{
		--j;
		++i;
		
		swapElementR(alphabets, size);
	}
		
	
	
}


However, it is returning me an infinite loop despite having a base condition of i >= j. It is suppose to keep swapping and return me the swapped array until the condition i >= j condition is met.

Please do not ask me to try to do it iteratively as i managed to figure it out and i'm trying to practice and figure out recursive swap but was stucked.
For each call to swapElementR(), the program creates i and j as local variables to that specific call. Their values are not passed as arguments. As a result, the function essentially does the first step forever and never reaches a point where i is not zero and j is not size - 1.

You may want to look for a way to pass i and j down the chain.
Topic archived. No new replies allowed.