answersLogoWhite

0


Best Answer

#include<iostream>

#include<fstream>

int main()

{

std::ifstream infile ("example.txt", std::ios::in);

unsigned chars(0);

unsigned words(0);

unsigned lines(0);

std::string delim("\t\n ");

char ch(0);

char last(0);

if (!infile.good())

{

std::cerr << "The filename is invalid." << std::endl;

return -1;

}

while (infile.get(ch))

{

switch (ch)

{

case ('\n'):

++lines;

case (' '):

case ('\t'):

// only count words if the last char was not a word delimiter

if (delim.find(last) == std::string::npos)

++words;

default:

++chars;

}

last = ch;

}

infile.close();

std::cout << "Number of chars:\t" << chars << std::endl;

std::cout << "Number of words:\t" << words << std::endl;

std::cout << "Number of lines:\t" << lines<< std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

You shouldn't use a pointer for this unless you can guarantee the string is null-terminated. Use a std::string instead. The following program demonstrates both methods.

#include<iostream>

#include<string>

bool is_whitespace (const char c)

{

switch (c)

{

case (' '):

case ('\n'):

case ('\t'):

return true;

}

return false;

}

size_t count_words_unsafe (char* p)

{

// this version is unsafe as there's no guarantee

// the given pointer points to a null-terminated

// character array, resulting in undefined behaviour

// initialise the return value

size_t count=0;

// check for null pointer

if (p==nullptr)

return count;

// assume the first character is a whitespace

bool whitespace = true;

// iterate through each character

for ( ; *p!='\0'; ++p)

{

// is the current character a whitespace?

if (is_whitespace (*p))

{

// yes -- is the whitespace flag unset?

if (!whitespace)

{

// yes -- we've reached the end of a word

++count;

// set the whitespace flag

whitespace = true;

}

}

else

{

// unset the whitespace flag

whitespace = false;

}

}

// return the result

return count;

}

size_t count_words_safe (const std::string& s)

{

// this version is safe because std::string

// variables know their own length

// initialise the return value

size_t count=0;

// assume the first character is a whitespace

bool whitespace = true;

// iterate through each character

for (auto c : s)

{

// is the current character a whitespace?

if (is_whitespace (c))

{

// yes -- is the whitespace flag unset?

if (!whitespace)

{

// yes -- we've reached the end of a word

++count;

// set the whitespace flag

whitespace = true;

}

}

else

{

// unset the whitespace flag

whitespace = false;

}

}

// return the result

return count;

}

int main ()

{

// declare a C-style string

char* s

{

"\tThis is a paragraph made up of several lines. Words are\n"

"separated by white space characters, which includes single or\n"

"multiple spaces, tabs and newlines. There are 40 words in this\n"

"paragraph. Note that the first character is a whitespace.\n"

};

std::cout << "The paragraph:\n\n" << s << std::endl;

size_t safe_count = count_words_safe (s);

std::cout << "Safe count:\nThe paragraph has " << safe_count << " words.\n" << std::endl;

size_t unsafe_count = count_words_unsafe (s);

std::cout << "Unsafe count:\nThe paragraph has " << unsafe_count << " words.\n" << std::endl;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to count the number of characters words and lines in given text c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Program to count the number of characters and words in the line?

in linux wc -l filename will count the lines and wc will count the letters


Algoritm source code for write a c-program to count lines words and characters in a given text?

yyu5uty


It 4-1 solved question papers November -2008?

Write a program to count the number of IS in any number in register B and put the count in R5.


How do you write c program to accept a string from the console and count number of vowels constants digits tabs and blank spaces in a string?

Read the characters one at a time, and write an "if" for each of the cases. In each case, if the condition is fulfilled, increment the corresponding counter variable.


Program to count the number of numbers in an array using 8085 microprocessor?

A program which is used to count the number of numbers in an array using a 8085 microprocessor is known as a assembly language program.


Which computer program can tell you how many of each characters are in a document aka How many As Bs Cs Etc?

A word processing application, such as Microsoft Word, can be used to count characters. For example, you can do an Advanced Search individually for each character in the document: A, B, C, etc. and then write down the number of how many of each are found in the search results.


Write a program to get table of a number?

#include "stdio.h" #include "conio.h" #define TABLE_UP_TO_20 20 void table_of_a_number(int number); int main() { int i = 0x00; printf("Enter a positive number in decimal whose table has to be generated"); scanf("%d",&amp;i); table_of_a_number(i); return 0; } void table_of_a_number(int number) { int count = 0x00; for(count = 0x01;count &lt;=TABLE_UP_TO_20 ;count++) { /* THis will print the table*/ printf(" %d * %d = %d\n", number,count, (number*count)); }


Write a program that ask the user to enter a number n and display the first n even numbers?

#include "stdio.h" int main() { unsigned int number, count; printf("Enter the Number \t"); scanf("%d", &amp;number); printf("The even numbers are: \n"); for(count = 0x01; (count &lt; number &amp;&amp; number!= 0x00)) { if(count%2) { }else { printf("%d\n", count); } count++; } return 0; }


How would you write a program that counts the number of lowercase in a character?

I would use a loop like this: const char *p= str-1; size_t count= 0; while (*++p) if (islower (*p)) ++count;


How do you write a c program that counts number of blanks in a text file using standard io?

int count_whitespace (FILE* input) { int c, count=0; while (( c = getc(input) ) != EOF ) if ((char) c==' ') ++count; return count; }


To count the number of characters in the given text file in unix?

Look at the "wc" command's man page, it will give you a count of all characters, including the newline character.


How do you write a program in c that will count from 1 to 10 and its square for each count?

int i; for (i=1; i&lt;=10; i++) printf ("%d %d\n", i, i*i);