We have to count the 3 letter,4letter and 5letter words from a file and print the number of 3 letter,4letter and 5letter words. Delimiter is space,tab,hifen.Also we should not consider the line in the file after we encounter # in that line.(ie after # we should not consider the portion of line)

Showing Answers 1 - 9 of 9 Answers

alok

  • Jul 10th, 2005
 

hii  
Any one can post the answer of this question. 
plzz 
bye

  Was this answer useful?  Yes

ajeet

  • Jul 21st, 2005
 

hi  
use fscanf(....) to read the string ,calculate length using strlen function and increment the counter for respective words. 
we will have to take care of #. 

  Was this answer useful?  Yes

Padiyar

  • Aug 2nd, 2005
 

For #, we can use the String.indexOf("#") method to check the # substring

  Was this answer useful?  Yes

Rajaram

  • Oct 14th, 2005
 

There are two ways to do this program. Both the ways are shown below.

1)

using System;

using System.IO;

namespace nmsFileOperations

{

class clzFileOperations

{

[STAThread]

static void Main(string[] args)

{

string strLine;

int Wrd1=0, Wrd2=0, Wrd3=0;

int Wrd4=0, Wrd5=0, Wrd6=0;

StreamReader sr= new StreamReader(@"c:\a.txt");

strLine = sr.ReadLine();

while(strLine!=null)

{

if (strLine.Trim().Substring(0,1)!="#")

{

string [] strSplit = strLine.Trim().Split(new char[] {' ' ,'.',';',':','\t'});

foreach(string str in strSplit)

{

switch(str.Length)

{

case 1:

Wrd1++;

break;

case 2:

Wrd2++;

break;

case 3:

Wrd3++;

break;

case 4:

Wrd4++;

break;

case 5:

Wrd5++;

break;

case 6:

Wrd6++;

break;

}

}

}

strLine = sr.ReadLine();

}

Console.WriteLine(" The Count of One Letters Word is {0} ", + Wrd1);

Console.WriteLine(" The Count of Two Letters Word is {0} ", + Wrd2);

Console.WriteLine(" The Count of Three Letters Word is {0} ", + Wrd3);

Console.WriteLine(" The Count of Four Letters Word is {0} ", + Wrd4);

Console.WriteLine(" The Count of Five Letters Word is {0} ", + Wrd5);

Console.WriteLine(" The Count of Six Letters Word is {0} ", + Wrd6);

}

}

}

2)

using System;

using System.IO;

namespace nmsFileHandling

{

class clzFileHandling

{

[STAThread]

static void Main(string[] args)

{

string strLine;

string [] strWord;

int Wrd1=0, Wrd2=0, Wrd3=0, Wrd4=0;

int Wrd5=0, Wrd6=0;

StreamReader sr= new StreamReader(@"c:\a.txt");

strLine = sr.ReadLine().Trim();

while (strLine!=null)

{

if (strLine.Trim().Substring(0,1)!="#")

{

//strWord = strLine.Split(' ');

strWord = strLine.Split(new char[] {' ' ,'.',';',':','\t'});

for (int i=0; i<=strWord.Length-1; i++)

{

switch (strWord[i].Trim().Length)

{

case 1:

Wrd1++;

break;

case 2:

Wrd2++;

break;

case 3:

Wrd3++;

break;

case 4:

Wrd4++;

break;

case 5:

Wrd5++;

break;

case 6:

Wrd6++;

break;

}

}

}

strLine = sr.ReadLine();

}

Console.WriteLine("\nTotol Number of One word Letters is {0}", Wrd1);

Console.WriteLine("\nTotol Number of Two word Letters is {0}", Wrd2);

Console.WriteLine("\nTotol Number of Three word Letters is {0}", Wrd3);

Console.WriteLine("\nTotol Number of Four word Letters is {0}", Wrd4);

Console.WriteLine("\nTotol Number of Five word Letters is {0}", Wrd5);

Console.WriteLine("\nTotol Number of Six word Letters is {0}", Wrd6);

}

}

}

  Was this answer useful?  Yes

Rajaram

  • Oct 14th, 2005
 

Write a program to Open a file and read each and every line which doesn't start from # symbol and display the following details 

Count the Number of words which has

1Letters of word

2Letters of word

3Letters of word

4Letters of word

And Each word splited by ":,\t ."

1)

using System;

using System.IO;

namespace nmsFileOperations

{

class clzFileOperations

{

[STAThread]

static void Main(string[] args)

{

string strLine;

int Wrd1=0, Wrd2=0, Wrd3=0;

int Wrd4=0, Wrd5=0, Wrd6=0;

StreamReader sr= new StreamReader(@"c:\a.txt");

strLine = sr.ReadLine();

while(strLine!=null)

{

if (strLine.Trim().Substring(0,1)!="#")

{

string [] strSplit = strLine.Trim().Split(new char[] {' ' ,'.',';',':','\t'});

foreach(string str in strSplit)

{

switch(str.Length)

{

case 1:

Wrd1++;

break;

case 2:

Wrd2++;

break;

case 3:

Wrd3++;

break;

case 4:

Wrd4++;

break;

case 5:

Wrd5++;

break;

case 6:

Wrd6++;

break;

}

}

}

strLine = sr.ReadLine();

}

Console.WriteLine(" The Count of One Letters Word is {0} ", + Wrd1);

Console.WriteLine(" The Count of Two Letters Word is {0} ", + Wrd2);

Console.WriteLine(" The Count of Three Letters Word is {0} ", + Wrd3);

Console.WriteLine(" The Count of Four Letters Word is {0} ", + Wrd4);

Console.WriteLine(" The Count of Five Letters Word is {0} ", + Wrd5);

Console.WriteLine(" The Count of Six Letters Word is {0} ", + Wrd6);

}

}

}

  Was this answer useful?  Yes

Amit Rathi

  • Mar 4th, 2006
 

Dim str As StringDim sr As New System.IO.StreamReader("c:/temp.txt") str = sr.ReadToEnd Dim ar() As String = str.Split(" ") For i As Integer = 0 To ar.Length - 1 If (ar(i).Length = 3) Then MsgBox(ar(i)) End If Next

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions