c - How to change first letter to uppercase in array of pointers to strings? -
i have arrays of pointers char arrays below.
char *ptrarticle[]={"the","a","one","some"};
i trying randomize sentence this:
"the girl skipped under boy."
so have make first words first character uppercase. not seem work. compiler not give errors not work intended also. still lowercase. can give me advice?
toupper(*ptrarticle[articlerandomindex]); #include <stdio.h> #include<stdlib.h> #include<time.h> #include <string.h> #include <ctype.h> int main(void){ int articlerandomindex; int nounrandomindex; int verbrandomindex; int prepositionrandomindex; int secondarticlerandomindex; int secondnounrandomindex; char sentence[200]; //array of pointers char arrays char *ptrarticle[]={"the","a","one","some"}; char *ptrnoun[]={"boy","girl","dog","town","car"}; char *ptrverb[]={"drove","jumped","ran","walked","skipped"}; char *ptrpreposition[]={"to","from","over","under","on"}; srand(time(null)); articlerandomindex=rand()%4; nounrandomindex=rand()%5; verbrandomindex=rand()%5; prepositionrandomindex=rand()%5; secondarticlerandomindex=rand()%4; secondnounrandomindex=rand()%5; toupper(*ptrarticle[articlerandomindex]); strcpy(sentence,ptrarticle[articlerandomindex]); strcat(sentence," "); strcat(sentence,ptrnoun[nounrandomindex]); strcat(sentence," "); strcat(sentence,ptrverb[verbrandomindex]); strcat(sentence," "); strcat(sentence,ptrpreposition[prepositionrandomindex]); strcat(sentence," "); strcat(sentence,ptrarticle[secondarticlerandomindex]); strcat(sentence," "); strcat(sentence,ptrnoun[secondnounrandomindex]); strcat(sentence,"."); puts(sentence); getch(); }
first of all, toupper
function returns upper case character.
secondly, why not toupper
on first character in string construct?
e.g.
sentence[0] = toupper(sentence[0]); puts(sentence);
this way can use randomizing code multiple times without modifying actual string use construction of sentence. also, won't try modify string literal, read only.
Comments
Post a Comment