C Program to Capitalize First Letter of Each Word in a File

In this program, we will write a C program to read a file and capitalize the first letter of each word present in the file. The program will read the content of the file, modify it by capitalizing the first letter of each word, and then write the modified content back to the file.

Problem statement

Write a C program that reads the contents of a file, capitalizes the first letter of each word in the file, and writes the modified content back to the same file.

C Program to Capitalize First Letter of Each Word in a File

#include <stdio.h>
#include <ctype.h>

void capitalizeFirstLetter(char *str) {
    int capitalize = 1;
    
    while (*str) {
        if (isspace(*str)) {
            capitalize = 1;
        } else if (capitalize) {
            *str = toupper(*str);
            capitalize = 0;
        }
        
        str++;
    }
}

int main() {
    FILE *file;
    char filename[100];
    char buffer[1000];
    
    printf("Enter the name of the file: ");
    scanf("%s", filename);
    
    // Open the file in read mode
    file = fopen(filename, "r");
    
    if (file == NULL) {
        printf("Unable to open the file.\n");
        return 1;
    }
    
    // Read the content of the file
    fgets(buffer, sizeof(buffer), file);
    
    // Close the file
    fclose(file);
    
    // Capitalize the first letter of each word
    capitalizeFirstLetter(buffer);
    
    // Open the file in write mode
    file = fopen(filename, "w");
    
    if (file == NULL) {
        printf("Unable to open the file.\n");
        return 1;
    }
    
    // Write the modified content back to the file
    fputs(buffer, file);
    
    // Close the file
    fclose(file);
    
    printf("Successfully capitalized the first letter of each word in the file.\n");
    
    return 0;
}

How it works

  1. The program starts by including the necessary header files: stdio.h for standard input/output functions and ctype.h for character handling functions.
  2. The capitalizeFirstLetter function is defined to capitalize the first letter of each word in a given string. It takes a character array (string) as input and modifies it in-place.
  3. In the main function, a file pointer file and character arrays filename and buffer are declared.
  4. The user is prompted to enter the name of the file to be processed.
  5. The file is opened in read mode using fopen function. If the file does not exist or cannot be opened, an error message is displayed, and the program terminates.
  6. The content of the file is read using fgets function and stored in the buffer array.
  7. The file is closed using fclose function.
  8. The capitalizeFirstLetter function is called with the buffer array as an argument to capitalize the first letter of each word.
  9. The file is opened again, but this time in write mode.
  10. If the file cannot be opened in write mode, an error message is displayed, and the program terminates.
  11. The modified content stored in the buffer array is written back to the file using fputs function.
  12. The file is closed.
  13. Finally, a success message is displayed indicating that the first letter of each word has been capitalized in the file.

Input / output

C Program to Capitalize First Letter of Each Word in a File

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program finds the Greatest Common Divisor (GCD) of two given numbers. Problem Statement Write a C program that...
This C program calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0....
This C program allows you to find the length of a linked list, which is the number of nodes present...