Interview Question
SDE1sCountry: India
Interview Type: Written Test
#include <stdio.h>
void main()
{
FILE *f1;
int char, word, line;
char c;
while ((c = getc(f1)) != EOF) {
if (c != '\0')
char ++;
else
word++;
if (c == '\n')
line++;
}
printf("char=%d word=%d line=%d", char, word, line)
}
int getwordcount(const string &str)
{
int res = 0;
string prestr = "";
for(int i = 0; i < str.length(); ++i)
{
if(ispunctuationorseperatechar(str[i]))
{
if(prestr != "") res++;
prestr = "";
}
else
{
prestr += s[i];
}
}
return res;
}
void printCounts(string &filename)
{
if(filename == "") return;
int linecount = 0, wordcount = 0, charcount = 0;
ifstream ifs(filename.c_str());
while(!ifs.eof())
{
string line;
getline(ifs, line);
if(line.length() <= 1) continue;
linecount++;
charcount += line.length();
wordcount += getwordcount(line);
}
ifs.close();
cout << linecount<<","<<wordcount<<","<<charcount<<endl;
}
- ts_v_murthy December 14, 2013