KEMBAR78
source code which create file and write into it | DOCX
import java.util.*;
import java.io.*;
public class TextFileInfoPrinter
{
public static void main(String[]args) throws FileNotFoundException
{
Scanner console= new Scanner(System.in);
System.out.println("File to be read: ");
String inputFile = console.next();
File file = new File(inputFile);
Scanner in = new Scanner(file);
int words = 0;
int lines = 0;
int chars = 0;
while(in.hasNext())
{
in.next();
words++;
}
while(in.hasNextLine())
{
in.nextLine();
lines++;
}
while(in.hasNextByte())
{
in.nextByte();
chars++;
}
System.out.println("Number of lines: " + lines);
System.out.println("Number of words: " + words);
System.out.println("Number of characters: " + chars);
}
}
Programto countno ofcharacters
ifstream infile;
//char mystring[6];
//char mystring[20];
int main()
{
infile.open("file.txt");
if(infile.fail())
{
cout << " Error " << endl;
}
int numb_char=0;
char letter;
while(!infile.eof())
{
infile.get(letter);
cout << letter;
numb_char++;
}
cout << " the number of characters is :" << numb_char << endl;
infile.close();
return 0;
}
Countsno of lines,words andcharacters
#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
// start of main program
int main()
{
string filename;
cout<<"Please enter the data filename: ";
// reads the filename fromthe user
cin>>filename;
// open the filefor input.
ifstream infile(filename, std::ifstream::in);
// create the stream in read-only mode
if(!infile){
cout << "Cannot open file for reading.n";
_getch();
return 1;
}
// declares character and integer variables
char ch,c;
int count=1;
int i=0;
int count1=1;
// running the loop until file willend
while(infile.get(ch))
{
cout<<ch;
if(ch==' ')
{
count++;
count1++;
}
else if(ch==' ')
count1++;
i=i+1;
}
cout<<"";
// display the number of character, words and line
cout<<"nNumber of characters: "<<i-(count-1+count1-1)<<"";
cout<<"nNumber of words: "<<count1<<"";
cout<<"nNumber of lines: "<<count<<"";
// closes the file
infile.close();
_getch();
return 0;
}
Write a c programto count the numberof character,numberof linesina file
#include<stdio.h>
#include<conio.h>
void main()
{
int noc=0,now=0,nol=0;
FILE *fw,*fr;
char fname[20],ch;
clrscr();
printf("n enter the source file name");
gets(fname);
fr=fopen(fname,"r");
if(fr==NULL)
{
printf("n error n");
exit(0);
}
ch=fgetc(fr);
while(ch!=EOF)
{
noc++;
if(ch==' ');
now++;
if(ch=='n')
{
nol++;
now++;
}
ch=fgetc(fr);
}
fclose(fr);
printf("n total no of character=%d",noc);
printf("n total no of words=%d",now);
printf("n total no of lines=%d",nol);
getch();
}
Occurences of aword in text file
#include<iostream.h>
#include<fstream.h>
#include<string.h>
int main()
{
ifstream fin("my_data.txt");//opening text file
int count=0;
char ch[20],c[20];
cout<<"Enter a word to count:";
gets(c);
while(fin)
{
fin>>ch;
if(strcmp(ch,c)==0)
count++;
}
cout<<"Occurrence="<<count<<"n";
fin.close();//closing file
return 0;
}
Question2
/*************************************************
* C program to count no of lines, wordsand *
* characters in a file. *
*************************************************/
#include <stdio.h>
int main()
{
FILE *fp;
char filename[100];
char ch;
int linecount, wordcount,charcount;
// Initialize counter variables
linecount = 0;
wordcount= 0;
charcount = 0;
// Prompt user to enter filename
printf("Enter a filename :");
gets(filename);
// Open file in read-only mode
fp = fopen(filename,"r");
// If file opened successfully, then write the string to file
if ( fp )
{
//Repeat until End Of File character is reached.
while ((ch=getc(fp))!=EOF){
// Increment character count if NOTnew line or space
if (ch!= ' ' && ch != 'n') { ++charcount; }
// Increment word count if new line or space character
if (ch == ' ' ||ch == 'n') { ++wordcount;}
// Increment line count if new line character
if (ch == 'n'){ ++linecount; }
}
if (charcount > 0) {
++linecount;
++wordcount;
}
}
else
{
printf("Failed to open the filen");
}
printf("Lines : %d n", linecount);
printf("Words: %d n", wordcount);
printf("Characters : %d n", charcount);
getchar();
return(0);
}
C Programto count characters,lines,spaces& tabs in a file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char a[20];
int nol=0,not=0,nob=0,noc=0;
char c;
clrscr();
printf("Enter the name of File:n");
gets(a);
if((fp=fopen(a,"r"))==NULL)
{
printf("File dosen't exist.");
}
else
{
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
noc++;
if(c==' ')
nob++;
if(c=='n')
nol++;
if(c=='t')
not++;
}
}
fclose(fp);
printf("Number of characters = %dn",noc);
printf("Number of blanks = %dn",nob);
printf("Number of tabs = %dn",not);
printf("Number of lines = %dn",nol);
getch();
}
Programcountsno occurrences ofcertainwordintext file
int WordCount ::countWords(string wrd)
{
int counter=0;
string temp = "";
while (getline(*file,temp))
{
for (int i = 0; i < temp.length();i++)
{
if (tolower(temp[i]+ temp[i+1] + temp[i+2]) == (wrd[0]+ wrd[1] + wrd[2]))
{
counter++;
}
else if (toupper(temp[i] + temp[i+1] + temp[i+2]) == toupper(wrd[0] + wrd[1] + wrd[2]))
{
counter++;
}
}
}
return counter;
}

source code which create file and write into it

  • 1.
    import java.util.*; import java.io.*; publicclass TextFileInfoPrinter { public static void main(String[]args) throws FileNotFoundException { Scanner console= new Scanner(System.in); System.out.println("File to be read: "); String inputFile = console.next(); File file = new File(inputFile); Scanner in = new Scanner(file); int words = 0; int lines = 0; int chars = 0; while(in.hasNext()) { in.next(); words++; } while(in.hasNextLine()) { in.nextLine(); lines++; } while(in.hasNextByte()) {
  • 2.
    in.nextByte(); chars++; } System.out.println("Number of lines:" + lines); System.out.println("Number of words: " + words); System.out.println("Number of characters: " + chars); } } Programto countno ofcharacters ifstream infile; //char mystring[6]; //char mystring[20]; int main() { infile.open("file.txt"); if(infile.fail()) { cout << " Error " << endl; } int numb_char=0; char letter; while(!infile.eof())
  • 3.
    { infile.get(letter); cout << letter; numb_char++; } cout<< " the number of characters is :" << numb_char << endl; infile.close(); return 0; } Countsno of lines,words andcharacters #include<iostream> #include<fstream> #include<string> #include<conio.h> using namespace std; // start of main program int main() { string filename; cout<<"Please enter the data filename: ";
  • 4.
    // reads thefilename fromthe user cin>>filename; // open the filefor input. ifstream infile(filename, std::ifstream::in); // create the stream in read-only mode if(!infile){ cout << "Cannot open file for reading.n"; _getch(); return 1; } // declares character and integer variables char ch,c; int count=1; int i=0; int count1=1; // running the loop until file willend while(infile.get(ch)) { cout<<ch; if(ch==' ') {
  • 5.
    count++; count1++; } else if(ch==' ') count1++; i=i+1; } cout<<""; //display the number of character, words and line cout<<"nNumber of characters: "<<i-(count-1+count1-1)<<""; cout<<"nNumber of words: "<<count1<<""; cout<<"nNumber of lines: "<<count<<""; // closes the file infile.close(); _getch(); return 0; } Write a c programto count the numberof character,numberof linesina file #include<stdio.h> #include<conio.h>
  • 6.
    void main() { int noc=0,now=0,nol=0; FILE*fw,*fr; char fname[20],ch; clrscr(); printf("n enter the source file name"); gets(fname); fr=fopen(fname,"r"); if(fr==NULL) { printf("n error n"); exit(0); } ch=fgetc(fr); while(ch!=EOF) { noc++; if(ch==' '); now++; if(ch=='n')
  • 7.
    { nol++; now++; } ch=fgetc(fr); } fclose(fr); printf("n total noof character=%d",noc); printf("n total no of words=%d",now); printf("n total no of lines=%d",nol); getch(); } Occurences of aword in text file #include<iostream.h> #include<fstream.h> #include<string.h> int main() { ifstream fin("my_data.txt");//opening text file int count=0;
  • 8.
    char ch[20],c[20]; cout<<"Enter aword to count:"; gets(c); while(fin) { fin>>ch; if(strcmp(ch,c)==0) count++; } cout<<"Occurrence="<<count<<"n"; fin.close();//closing file return 0; } Question2 /************************************************* * C program to count no of lines, wordsand * * characters in a file. * *************************************************/ #include <stdio.h>
  • 9.
    int main() { FILE *fp; charfilename[100]; char ch; int linecount, wordcount,charcount; // Initialize counter variables linecount = 0; wordcount= 0; charcount = 0; // Prompt user to enter filename printf("Enter a filename :"); gets(filename); // Open file in read-only mode fp = fopen(filename,"r"); // If file opened successfully, then write the string to file if ( fp ) { //Repeat until End Of File character is reached. while ((ch=getc(fp))!=EOF){ // Increment character count if NOTnew line or space if (ch!= ' ' && ch != 'n') { ++charcount; }
  • 10.
    // Increment wordcount if new line or space character if (ch == ' ' ||ch == 'n') { ++wordcount;} // Increment line count if new line character if (ch == 'n'){ ++linecount; } } if (charcount > 0) { ++linecount; ++wordcount; } } else { printf("Failed to open the filen"); } printf("Lines : %d n", linecount); printf("Words: %d n", wordcount); printf("Characters : %d n", charcount); getchar();
  • 11.
    return(0); } C Programto countcharacters,lines,spaces& tabs in a file. #include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { FILE *fp; char a[20]; int nol=0,not=0,nob=0,noc=0; char c; clrscr(); printf("Enter the name of File:n"); gets(a); if((fp=fopen(a,"r"))==NULL) { printf("File dosen't exist."); } else { while(1) { c=fgetc(fp); if(c==EOF) break;
  • 12.
    noc++; if(c==' ') nob++; if(c=='n') nol++; if(c=='t') not++; } } fclose(fp); printf("Number ofcharacters = %dn",noc); printf("Number of blanks = %dn",nob); printf("Number of tabs = %dn",not); printf("Number of lines = %dn",nol); getch(); } Programcountsno occurrences ofcertainwordintext file int WordCount ::countWords(string wrd) { int counter=0; string temp = ""; while (getline(*file,temp)) { for (int i = 0; i < temp.length();i++) { if (tolower(temp[i]+ temp[i+1] + temp[i+2]) == (wrd[0]+ wrd[1] + wrd[2]))
  • 13.
    { counter++; } else if (toupper(temp[i]+ temp[i+1] + temp[i+2]) == toupper(wrd[0] + wrd[1] + wrd[2])) { counter++; } } } return counter; }