this question is in a book im working from (which i have copied word for word):
Re-write the program ex_06.c found in chapter 9 on one-dimensional arrays, to encompass a function with the following prototype.
Code:
char *to_upper(char *array);
where the function to_upper converts any lower-case letters found in the array and returns a pointer to a string containing upper-case characters. The advantage of this function is that the contents of the original array need not be changed.
chapter9_ex_06.c
original program written by author:
Code:
#include <stdio.h>
#define to_upper(c) ('a'<= (c) && (c) <= 'z' ? (c)-32: (c))
main()
{
const char null = 0;
char text[255];
int index;
printf("input a line of text\n");
gets(text);
for (index = 0; text[index] != null; index++)
text[index] = to_upper(text[index]);
printf("capitalized text\n%s", text);
}
my program in answer to question:
Code:
#include <stdio.h>
char *to_upper(char *array)
{
int i = 0;
while (array[i]){
if ((array[i] >= 'a') && (array[i] <= 'z'))
array[i] = (array[i] - 32);
i++;
}
return array;
}
main()
{
char input[25];
printf("input a line of text\n");
gets(input);
to_upper(input);
printf("capitalized text\n%s", input);
}
it works but not how the question requests,
my version works without returning a pointer as the "array" gets passed by reference and thus gets changed in the function to_upper so not needing a return at all. A lso i must be missing something else because the author mentions that the contents of the original array need not be changed, my version always changes the original array. I have been reading about pointers but still dont understand this one.
thanks
luigi