Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program that takes a string from user and prints the total number of characters, words and lines present in the string.

Posted By: Trina King     Category: C Programming     Views: 8111

Write a program that takes a string from user and prints the total number of characters, words and lines present in the string.

Code for Program that takes a string from user and prints the total number of characters, words and lines present in the string. in C Programming

#include<stdio.h>
#include<conio.h>
void main()
{
        char a[50];
        int i,j,n,totword=1,totchar=0,totline=1;
        clrscr();
        printf("\n Please Give The Value of N:  ");
        scanf("%d",&n);

        for(i=0;i<n;i++)
        {
            flushall();
            printf("\n enter value of a[%d] :",i);
            scanf("%c",&a[i]);
        }

        for(i=0;i<n;i++)
        {
            if(a[i]==32 || a[i]=='.' || a[i]=='\0')
            {
                totword++;
                totchar++;
            }
            elseif(a[i]=='\n')
            {
                totline++;
                totword++;
            }

            else
                totchar++;
        }

        printf("\n\nTHE TOTAL STRING IS \n\n");
        for(i=0;i<n;i++)
        printf("%c",a[i]);
        printf("\nTHE TOTAL NO. OF CHARACTERS %d .",totchar);
        printf("\nTHE TOTAL NO. OF WORDS %d .",totword);
        printf("\nTHE TOTAL NO. OF LINES %d .",totline);
        getch();
}


********************** OUTPUT ***********************************************


    Please Give The Value of N:  6

     enter value of a[0] :A

     enter value of a[1] :S

     enter value of a[2] :D

     enter value of a[3] :
    
     enter value of a[4] :D

     enter value of a[5] :F

    THE TOTAL STRING IS

    ASD DF
    THE TOTAL NO. OF CHARACTERS 6 .
    THE TOTAL NO. OF WORDS 2 .
    THE TOTAL NO. OF LINES 1 .

*********************************************************************************
  
Share: 



Trina King
Trina King author of Program that takes a string from user and prints the total number of characters, words and lines present in the string. is from Los Angeles, United States.
 
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!