//C code to count number of words spaces and white characters
#include <stdio.h>
int main()
{
char str[100];//input string with size 100
int words=0,newline=0,characters=0; // counter variables
scanf("%[^~]",&str);//scanf formatting
for(int i=0;str[i]!='\0';i++)
{
if(str[i] == ' ')
{
words++;
}
else if(str[i] == '\n')
{
newline++;
words++;//since with every next line new words start. corner case 1
}
else if(str[i] != ' ' && str[i] != '\n'){
characters++;
}
}
if(characters > 0)//Corner case 2,3.
{
words++;
newline++;
}
printf("Total number of words : %d\n",words);
printf("Total number of lines : %d\n",newline);
printf("Total number of characters : %d\n",characters);
return 0;
}
/*C - Convert All Characters in Uppercase of a File using C Program.*/
#include <stdio.h>
#include <ctype.h>
int main(){
//file nane
const char *fileName="sample.txt";
//file pointers
FILE *fp,*fp1;
//to store read character
char ch;
//open file in read mode
fp=fopen(fileName,"r");
if(fp==NULL){
printf("Error in opening file.\n");
return -1;
//create temp file
fp1=fopen("temp.txt","w");
if(fp1==NULL){
printf("Error in creating temp file.\n");
return -1;
//read file from one file and copy
//into another in uppercase
while((ch=fgetc(fp))!=EOF){
if(islower(ch)){
ch=ch-32;
//write character into temp file
putc(ch,fp1);
fclose(fp);
fclose(fp1);
//rename temp file to sample.txt
rename("temp.txt","sample.txt");
//remove temp file
remove("temp.txt");
//now, print content of the file
//open file in read mode
fp=fopen(fileName,"r");
if(fp==NULL){
printf("Error in opening file.\n");
return -1;
printf("Content of file\n");
while((ch=getc(fp))!=EOF){
printf("%c",ch);
}
printf("\n");
fclose(fp);
return 0;
Copy the file contents:
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
printf("Cannot open file %s \n", filename);
exit(0);
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
fputc(c, fptr2);
c = fgetc(fptr1);
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;