Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Program that counts number of line, number of words and number characters in specified string

Posted By: Easy Tutor     Category: C Programming     Views: 27218

Write a Program that counts

1) Number of Lines
2) Number of Words
3) Number of Characters

in given string.

Code for Program that counts number of line, number of words and number characters in specified string in C Programming

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#define MAX_ROW 5
#define MAX_COL 80


void main(){
    char name[MAX_ROW][MAX_COL],c;
    int lines=1; //bcoz. first line will be left to count.int words=1; //bcoz. first word will be left to count.int chars=1; //bcoz. first char will be left to count.
    clrscr();
    cout<<"===Input Status===\n";
    cout<<"Enter string termanate by # : ";
    cin.get(c);

    //Finding no. of lineswhile(c != '#'){
        cin.get(c);
        chars++;
        if(c==' ' || c=='\n')
            words++;
        if(c=='\n')
            lines++;
    }

    cout<<"\n"<<setw(20)<<"Particulars"<<setw(20)<<"Details\n";
    cout<<"-------------------------------------\n";

    cout.setf(ios::left,ios::adjustfield);
    cout<<"\n"<<setw(20)<<"No. of lines  ";
    cout.setf(ios::right,ios::adjustfield);
    cout<<setw(15)<<lines;

    cout.setf(ios::left,ios::adjustfield);
    cout<<"\n"<<setw(20)<<"No. of Words  ";
    cout.setf(ios::right,ios::adjustfield);
    cout<<setw(15)<<words;

    cout.setf(ios::left,ios::adjustfield);
    cout<<"\n"<<setw(20)<<"No. of Characters  ";
    cout.setf(ios::right,ios::adjustfield);
    cout<<setw(15)<<chars;

    getch();

}
  
Share: 



Easy Tutor
Easy Tutor author of Program that counts number of line, number of words and number characters in specified string is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!