Thursday, January 6, 2011

C Program to find a number of lines,words and characters from a given string

Coding:



#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
   {
int l=1,w=0,chars=1;
char c;
clrscr();
textcolor(9);
cprintf("\n********Terminate String by pressing the Tab Key followed by Enter Key ********");
printf("\n\nEnter the String :\n\n");
c=getchar();
while(c!= '\t')
{
c=getchar();
chars++;
if(c==' '|| c=='\n'||c=='.')
w++;
if(c=='\n')
l++;
if(c=='\t')
chars--;
}
textbackground(WHITE);
cprintf("\r\n--------------------------  ");
cprintf("\r\nNumber of Lines      =%d    ",l);
cprintf("\r\nNumber of Words      =%d    ",w+1);
cprintf("\r\nNumber of Characters =%d    ",chars);
cprintf("\r\n--------------------------  ");
getch();
}


Output:



No comments: