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

This C Program capitalizes first letter of every word in a file.

Here is source code of the C Program to capitalize first letter of every word in a file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Capitalize First Letter of every Word in a File
  3.  */
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. int to_initcap_file(FILE *); 
  8.  
  9. void main(int argc, char * argv[])
  10. {
  11.     FILE *fp1;
  12.     char fp[10];
  13.     int p;
  14.  
  15.     fp1 = fopen(argv[1], "r+");
  16.     if (fp1 == NULL)
  17.     {
  18.         printf("cannot open the file ");
  19.         exit(0);
  20.     }
  21.     p = to_initcap_file(fp1);
  22.     if (p == 1)    
  23.     {    
  24.         printf("success");
  25.     }
  26.     else
  27.     {
  28.         printf("failure");
  29.     }
  30.     fclose(fp1);
  31. }
  32.  
  33. /* capitalizes first letter of every word */
  34. int to_initcap_file(FILE *fp)
  35. {
  36.     char c;
  37.  
  38.     c = fgetc(fp);
  39.     if (c >= 'a' && c <= 'z')
  40.     {
  41.         fseek(fp, -1L, 1);
  42.         fputc(c - 32, fp);
  43.     }
  44.     while(c != EOF)    
  45.     {
  46.         if (c == ' ' || c == '\n')
  47.         {
  48.             c = fgetc(fp);
  49.             if (c >= 'a' && c <= 'z')
  50.             {
  51.                 fseek(fp, -1L, 1);
  52.                 fputc(c - 32, fp);
  53.             }
  54.         }
  55.         else
  56.         {
  57.             c = fgetc(fp);
  58.         }
  59.     }
  60.     return 1;
  61. }

$ cc file5.c
$ a.out sample
success
$ cat sample
Wipro Technologies
File Copy Function
Successfully Read

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.