EECE 230 Homework 4, Fall 2012
American University of Beirut · Introduction to Programming · 6 pages · Dr. Louay Bazzi
More EECE 230 files
Show the text of this homework
#include <iostream>
#include <fstream>
using namespace std;
int main(){
cout << "Please enter the name of a file:" << endl;
char filename [100];
cin>>filename; //assume name does not include whitespaces for simplcity
ifstream infile;
infile.open(filename);
if(!infile)
cout << "Cannot open file!"<<endl;
else {
int charcount=0, linecount=0;
char c;
infile.get(c);
while(!infile.eof()){
charcount ++;
if(c=='\n') linecount++;
infile.get(c);
}
//the number of lines exceeds the number of '\n' characters by 1
//assuming that the file is not empty (if empty then then by
convention
// the number of lines is zero)
if(charcount!=0)
linecount = linecount + 1;
infile.close();
infile.clear();// clear the end of file status to be able to use the
variable infile again
// An alternative method is to declare another variable infile2:
//
ifstream infile2;
// infile2.open(filename);
// and use it instead of the variable infile in what follows
infile.open(filename);
char singleword[1000]="";
int wordcount =0;
infile >> singleword;
if(strlen(singleword)!=0)
wordcount++; // condition needed to
handle the case when the file consists of
// whitespaces only (in this case the variable singleword will retain
its original value "", which has
// length 0). Note that if the file contains only one word, the end of
the file will be reached after the
// first extraction.
while(!infile.eof())
{
infile >> singleword;
wordcount++;
}
infile.close();
cout << "The number of characters is : " << charcount << endl;
cout << "The number of lines is : " << linecount << endl;
cout << "The number of words is : " << wordcount << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main(){
cout << "Please enter a string" << endl;
char str[100];
cin.get(str,99);
int i;
cout << "Please enter i" << endl;
cin >> i;
if(i<0 || i> strlen(str)-1) {
cout<<endl<<"i out of bounds! Program terminates \n";
return 0;
}
while(str[i]!='\0') {
str[i] = str[i+1]; // shift all the characters after i
//one step backward (including the null termination)
i = i + 1;
}
cout << str << endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str1[100];
char str2[50];
char discard;
cout<<"Enter first string:"<<endl;
cin.get(str1,100);
cin.get(discard);
cout<<endl<<"Enter second string:"<<endl;
cin.get(str2,50);
int l1 = strlen(str1);
int l2 = strlen(str2);
bool found;
for(int i=0;i<=l1-l2;i++)
{
// check in this loop if str2 is a substring of str1 starting at the
index i
// i.e., check if str2[0 1 ... l2-1] = str1[i i+1 ... i+l2-1]
// i.e., check if str2[j]=str1[i+j], for j=0 ... l2-1
found = true; // initalize found to true
for(int j=0;j<l2;j++)
if(str1[i+j]!=str2[j]) // if a mismatch is detected
{
found = false; // set found to false
break; // break inner loop
}
if(found) break; // break outer loop
}
// why do we have to stop at i=l1-l2?
// for i> l1-l2, str2 does not fit as a substring of str1 starting at
index i
if(found) cout<<"\""<<str2<<"\" is a substring of \""<<str1<<"\""<<endl;
else cout<<"\""<<str2<<"\" is not a substring of \""<<str1<<"\""<<endl;
return 0;
}
#include <iostream>
using namespace std;
#include <fstream>
int main()
{
char fileName1[100];
char fileName2[100];
char word1[100];
char word2[100];
cout<<"Enter file name:";
cin>>fileName1;
ifstream infile;
infile.open(fileName1);
if(!infile) {
cout << "Cannot open file "<<fileName1<<"! Program
terminates"<<endl;
return 0;
}
cout<<"Enter destination file name:";
cin>>fileName2;
ofstream outfile;
outfile.open(fileName2);
cout<<"Replace word:";
cin>>word1;
cout<<"with:";
cin>>word2;
char array[10000];// assume file size is at most 10000 bytes
// read infile into the array
int l1=0;//length of file in bytes
char c;
infile.get(c);
while(!infile.eof()) {
array[l1]=c;
infile.get(c);
l1++;
}
// no need to null terminate array since we have computed its
// length (l1) and we are using it as a temporary buffer
int l2 = strlen(word1);// l2 = length of word1
// do not confuse with length of word2 which is not used in this program
bool found;
int i=0;
while(i<l1)
{
if(i<=l1-l2) {
// As in problem 3, check in this loop if word1 is a substring of
array
// starting at the index i
// i.e., check if word1[j]=array[i+j], for j=0 ... l2-1
found = true; // initalize found to true
for(int j=0;j<l2;j++)
if(array[i+j]!=word1[j]) // if a mismatch is detected
{
found = false; // set found to false
break; // break inner loop
}
if(found) {
outfile<<word2; // replace with word2
i=i+l2; // jump l2 (the length of word1) chars in the
array
}
else {
outfile<<array[i];
i++;
}
}
else {
outfile<<array[i];
i++;
}
}
infile.close();
outfile.close();
return 0;
}