My understanding of pointing is so disappointing: Exercise 5b Chapter 13
Hey Guys, I'm working on what for me has been the best chapter so far in this book and it is kicking my butt but, I'm learning a ton. This code finds letters in a string and capitalizes them. It skips what it has to. The updated string is copied into a char array called 'new'.
Can someone please help me understand why I can't print out the first letter or the entire string at the end of this code?
BTW: I have to use the pointer arithmetic.
Code:
#include <stdio.h>
void capitalize(char str[], int n);
int n=20;
int main(){
char ch[]="ljS4e8sjHd67ds";
int size = sizeof(ch)/sizeof(ch[0]);
capitalize(ch, size);
return (0);
}
void capitalize(char str[], int n){
printf("%s\n",str); // prints entire string
char new[n];
char *p;
p=str;
printf("p=%s\n",p); // prints entire string
for(p=str; p<str+n; p++){
if(*p>64 && *p<91){
*new=*p;
}else if(*p>96 && *p<123){
*new=toupper(*p);
}else{
*new=*p;
}
printf("%c\n", *new); // *q and *new both work
}
printf("%c", new[0]); // not working: I expected L
printf("%s", new); // not working: I expected LJS4E8SJHD67DS
}
Thank you.