0% found this document useful (0 votes)
29 views

#Include Int Main : //C Code To Count Number of Words Spaces and White Characters

The C code counts the number of words, lines, and total characters in a string by using counters and scanf formatting to read the string. It iterates through the string, increments the respective counters for spaces, newlines, and other characters. It handles corner cases by incrementing the word and line counters if any characters are present. The counts are printed at the end. The second C code sample converts all characters in a file to uppercase. It opens the input file and a temporary file, reads each character and converts it to uppercase if lowercase before writing to the temporary file. It then renames the temporary file to the original file name and removes the temporary file. It prints the uppercase contents of the converted file.

Uploaded by

K SAI PRANEETH
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

#Include Int Main : //C Code To Count Number of Words Spaces and White Characters

The C code counts the number of words, lines, and total characters in a string by using counters and scanf formatting to read the string. It iterates through the string, increments the respective counters for spaces, newlines, and other characters. It handles corner cases by incrementing the word and line counters if any characters are present. The counts are printed at the end. The second C code sample converts all characters in a file to uppercase. It opens the input file and a temporary file, reads each character and converts it to uppercase if lowercase before writing to the temporary file. It then renames the temporary file to the original file name and removes the temporary file. It prints the uppercase contents of the converted file.

Uploaded by

K SAI PRANEETH
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

//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;

You might also like