Your main()
-function is missing a return type. According to the language standard main()
shall return an int
eger. When a function doesn't take arguments, it's parameter list should be void
, so:
int main(void)
Here
printf("How many words: ");
scanf("%d", &n);
you don't check if the user enters a number which is in range of your array word
. Always check user input! n
should be within the range [1, 100]
:
printf("How many words: ");
if (scanf("%d", &n) != 1 || n < 1 || 100 < n) {
// handle the input error, maybe exit the program
}
Please have a look at the reference for scanf()
. scanf()
returns the number of receiving arguments successfully assigned. So if scanf("%d", &n)
doesn't return 1
an int
eger couldn't be read.
printf("Enter the words one by one\n");
for (i = 1; i <= n; i++)
Arrays in C and C++ start at the index 0
and the last valid index is size - 1
. For your array word
the first valid index is word[0]
and the last is word[99]
. You have to iterate from i = 0
to i < n
:
printf("Enter the words one by one\n");
for (i = 0; i < n; ++i)
scanf("%s", word[i]);
Don't ever use the %s
format specifier without specifying a WIDTH
to limit the number of characters scanf()
assigns to the argument. Again, please have a look at the documentation for scanf()
.
scanf("%49s", word[i]);
Not specifying a WIDTH
is a Buffer Overflow waiting to happen. If the users input is longer than 49 characters the rest will stay in stdin
s buffer and will be read by the next call to an input function. If you don't want that to happen but rather ignore the rest you'd have to think about more sophisticated ways to handle input.
strlwr(word[i]);
Please note that strlwr()
is no standard library function. See strupr() and strlwr() in string.h part are of the ANSI standard?
for (i = 1; i <= n; i++)
toupper('word[i][1]');
Again, iterate from i = 0
to i < n
. The function toupper()
takes a character (well, actually an int
because it also handles EOF
) and if it is a lowercase character) it returns it's capitalized equivalent. You don't do anything with the returned value.
for (i = 0; i < n; ++i)
word[i][0] = toupper(word[i][0]); // assign the result of toupper()
for (i = 1; i <= n; i++)
printf("%s\n", word[i]);
With all that's been said already:
for (i = 0; i < n; ++i)
printf("%s\n", word[i]);
Please note that you can simply use puts()
if you want to print a string followed by a newline character.
Putting all of that together:
#include <stdlib.h> // EXIT_FAILURE
#include <stdio.h> // printf(), scanf(), puts()
#include <ctype.h> // toupper()
#include <string.h> // _strlwr() (Microsoft)
int main(void)
{
printf("How many words: ");
int n;
if (scanf("%d", &n) != 1 || n < 1 || 100 < n) {
puts("Input error. :(\n");
return EXIT_FAILURE;
}
char word[100][50];
puts("Enter the words one by one\n");
for (int i = 0; i < n; ++i)
{
if (scanf("%49s", word[i]) != 1) {
puts("Input error. :(\n");
return EXIT_FAILURE;
}
_strlwr(word[i]);
}
for (int i = 0; i < n; ++i)
word[i][0] = (char)toupper(word[i][0]);
for (int i = 0; i < n; ++i)
puts(word[i]);
}