I have been trying to uppercase the first letter of a string which is inside an array of strings I tried a lot of methods but none of them worked here is my code:
#include <stdio.h>
#include <stdlib.h>
int nw=0;
char words[30][50];
int main(){
printf("enter your words when you finish write b\n");
do{
nw++;
scanf("%s",&words[nw]);
}while(strcmp(&words[nw],"b")!=0);
printf("%s",toupper(&words[1][0]));
}
what should I do please help
For starters you need to include headers <ctype.h>
and <string.h>
#include <ctype.h>
#include <string.h>
There is no great sense to declare these variables
int nw=0;
char words[30][50];
in the file scope. They could be declared in main
.
Instead of this call
scanf("%s",&words[nw]);
you have to write at least
scanf("%s", words[nw]);
The condition in the do-while statement should look like
while( nw < 30 && strcmp(words[nw],"b") != 0 );
And instead of this call'
printf("%s",toupper(&words[1][0]));
you should write
words[1][0] = toupper( ( unsigned char )words[1][0] );
printf( "%s", words[1] );
Here is a demonstrative program.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char words[30][50] =
{
"hello", "world!"
};
for ( size_t i = 0; words[i][0] != '\0'; i++ )
{
words[i][0] = toupper( ( unsigned char )words[i][0] );
printf( "%s ", words[i] );
}
putchar( '\n' );
return 0;
}
The program output is
Hello World!