The following program is one I am writing for one of my classes. It is basically a proof of 3 functions in strings.h. The problem I am having is with the toupper loop you see in strcap. The problem is that it is printing nothing at all. The malloc statement is one I have never used before, so I am hoping I am using it correctly. Regardless, the printing of the string is the onnly problem right now. The other function, strcmp, hasn't been written yet, so dont worry about it. Although, with my notes on it so far, I am wondering if an if statement in a loop could be made to check each letter. It seems like it would be really drawn out and complex though.
Code:
# include <stdio.h>
# include <ctype.h>
# include <stdlib.h>
int strlen (char *s) { /*finds the length of the array*/
int counter = 1;
char *p;
for (p=s; *p; p++) {
counter++;
}
return counter;
}
int strncmp (char *s, char *t, int n) { /*compares the two arrays*/
}
char *strcap (char *str) { /*capitalizes all the letters in an array*/
char *result = (char *) malloc (250);
for (result=str; *result; result++) {
toupper(*result);
}
return result;
}
int main () {
char *str = "The Gators need to make it past the second round in March";
char *str1 = "The Gators";
char *str2 = "The Gators need to make it past the second round in March";
printf ("\n%s", str);
printf ("\n%d", strlen (str1));
printf ("\n%d", strncmp (str, str1, 20));
printf ("\n%d", strncmp (str, str1, 10));
printf ("\n%d", strncmp (str, str2, strlen (str)));
printf ("\n%s", strcap (str));
printf ("\n%s\n", str);
system("PAUSE");
return 0;
}