C program to capitalize string not working -
so, assignment, had finish code meant capitalize string.
what tried this:
#include <stdio.h> void capitalize(char *str) { int = 0; if (str[i] >= 97 && str[i] <= 122) { str[i] = str[i] - 32; } else { i++; } } void strcopy(char *str2, char *str1) { while (*str2) { *str1 = *str2; str2++; str1++; } *str1 = '\0'; } int main(int argc, char **argv) { char string1[100] = "this long string!"; char string2[100]; strcopy(string1,string2); capitalize(string2); printf("the original string \"%s\"\n", string1); printf("the capitalized string \"%s\"\n", string2); } however, when tried running code returned:
the original string "this long string!"
capitalized string "this long string!"
strcopy not seem issue, copies string1 correctly string2.
i don't understand why capitalize not working, seems should going through letter letter , changing uppercase, if letter falls within ascii code lowercase letter.
i appreciate helping point out error is.
thanks in advance!
the problem capitilize function doesn't have loop. ever capitalize first letter of string.
you on right track looking this:
for (int = 0; str[i] != '\0'; ++i) { // here check end condition of string. // ie. has null termination been reached? if (str[i] >= 'a' && str[i] <= 'z') { str[i] = str[i] - ('a' - 'a'); } }
Comments
Post a Comment