Created a program that converts one string to upper case, then another string to lower case. For some reason, if in the main function, i declare the first array on top of the second, the loop prints the whole array twice. It doesn't matter which array and function goes first, i've tested it out with both:
Code:
#include<stdio.h>
#include<ctype.h>
//first array is labeled one, second array is
//labeled two
void upperString(char one[])
{
int z = 0;
while(one[z])
{
one[z] = toupper(one[z]);
z++;
}
printf("%s\n", one);
}
void lowerString(char two[])
{
int x = 0;
while(two[x])
{
two[x] = tolower(two[x]);
x++;
}
printf("%s\n", two);
}
int main()
{
char one[4] = {'a', 'b', 'c', 'd'};
char two[4] = {'A', 'B', 'C', 'D'};
upperString(one);
lowerString(two);
}
Output:
ABCDABCD
abcd
The fix is simple, you execute the first function directly after you declare the first array:
Code:
int main()
{
char one[4] = {'a', 'b', 'c', 'd'};
upperString(one);
char two[4] = {'A', 'B', 'C', 'D'};
lowerString(two);
}
Output with change:
ABCD
abcd
...but it doesn't make any sense to me why the ordering matters, nowhere in the code am I telling either of the loops to print out the string twice in the above example. I'm really curious about why the ordering DOES matter.