C Program to Replace First Letter of every Word with Capital Letter

«
»
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. }

advertisement
$ 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

Here’s the list of Best Reference 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
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn