C Program to Capitalize First Letter of Each Word in String

This C Program replaces first letter of every word with caps.

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

  1. /*
  2.  * C Program to replace first letter of every word with caps
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. void main(int argc, char *argv[])
  8. {
  9.     FILE *fp1;
  10.     int return_val;
  11.  
  12.     if ((fp1 = fopen(argv[1],"r+")) =  = NULL)
  13.     {
  14.         printf("file cant be opened");
  15.         exit(0);
  16.     }
  17.     return_val = init_cap_file(fp1);
  18.     if (return_val == 1)
  19.     {
  20.         printf("\nsuccess");
  21.     }
  22.     else
  23.     {
  24.         printf("\n failure");
  25.     }
  26. }
  27.  
  28. int init_cap_file(FILE *fp1)
  29. {
  30.     char ch;
  31.  
  32.     ch = fgetc(fp1);
  33.     if (ch >= 97 && ch <= 122)
  34.     {
  35.         fseek(fp1, -1L, 1);
  36.         fputc(ch - 32, fp1);
  37.     }
  38.     while (ch != EOF)
  39.     {
  40.         if (ch = = ' '|| ch == '\n')
  41.         {
  42.             ch = fgetc(fp1);
  43.             if (ch >= 97 && ch <= 122)
  44.             {
  45.                 fseek(fp1, -1L, 1);
  46.                 fputc(ch - 32, fp1);
  47.             }
  48.         }
  49.         else
  50.             ch = fgetc(fp1);
  51.     }
  52.     return 1;
  53. }

$ vi file5test
$ cat file5test
chandana ravella
chanikya ravella
sree lakshmi ravella
sree ramulu ravella
$ cc file5.c
$ a.out file5test
 
success$ cat file5test
Chandana Ravella
Chanikya Ravella
Sree Lakshmi Ravella
Sree Ramulu Ravella

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.

If you find any mistake above, kindly email to [email protected]

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.