A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Capitalize in C page! Here, you'll find the source code for this program as well as a description of how the program works.
#include <ctype.h>
#include <stdio.h>
#include <string.h>
char *captialize(char str[])
{
for (int i = 0; i < strlen(str); i++)
if (i == 0)
str[i] = toupper(str[i]);
else
continue;
return str;
}
int main(int argc, char *argv[])
{
if (argc == 2 && strlen(argv[1]) != 0)
printf("%s\n", captialize(argv[1]));
else if (argc > 2)
printf("Use quotes around multiple strings.\n");
else
printf("Usage: please provide a string\n");
return 0;
}
Capitalize in C was written by:
This article was written by:
If you see anything you'd like to change or update, please consider contributing.
#include <ctype.h>
#include <stdio.h>
#include <string.h>
These lines import standard libraries so we can use built-in functions.
<ctype.h> provides character functions like toupper().<stdio.h> provides input/output functions like printf().<string.h> provides string utilities like strlen().int main(int argc, char *argv[])
This is where every C program starts. Here, argc represents the number of
command-line arguments, and argv represents an array of strings containing
those arguments. So, if we run:
./capitalize "hello"
then:
argc = 2argv[0] = "./capitalize"argv[1] = "hello"if (argc == 2 && strlen(argv[1]) != 0)
This ensures:
If the user provides no input, the program prints usage message. If there are
too many arguments, the program warns about quotes, because the user most
likely called the program using ./capitalize hello world instead of
./capitalize "hello world".
printf("%s\n", capitalize(argv[1]));
If input is valid, we call capitalize() with the first argument, then print
the result. In the function:
char *captialize(char str[])
{
for (int i = 0; i < strlen(str); i++)
if (i == 0)
str[i] = toupper(str[i]);
else
continue;
return str;
}
we traverse each letter in the string. If i = 0, then we capitalize the first
letter of the string using toupper, otherwise the string remains the same.
If we want to compile and run the program on a computer, we first need a C compiler installed. Common choices include GCC, Clang, and Microsoft Visual C++ (MSVC).
Once a compiler is available, we can open a terminal in the same directory as the source file and compile the program.
For example, using GCC:
gcc -o capitalize capitalize.c
./capitalize
or using Clang:
clang -o capitalize capitalize.c
./capitalize
On Windows with the Microsoft compiler:
cl capitalize.c
capitalize.exe