#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;
}
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;
}
yyu5uty
#include "stdio.h" int main() { unsigned int number, count; printf("Enter the Number \t"); scanf("%d", &number); printf("The even numbers are: \n"); for(count = 0x01; (count < number && number!= 0x00)) { if(count%2) { }else { printf("%d\n", count); } count++; } return 0; }
I would use a loop like this: const char *p= str-1; size_t count= 0; while (*++p) if (islower (*p)) ++count;
Look at the "wc" command's man page, it will give you a count of all characters, including the newline character.
.... String line = "This is example program with spaces"; String[] tokens = line.split(" "); System.out.println(tokens.length-1); .......
in linux wc -l filename will count the lines and wc will count the letters
yyu5uty
Write a program to count the number of IS in any number in register B and put the count in R5.
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.
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.
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.
#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",&i); table_of_a_number(i); return 0; } void table_of_a_number(int number) { int count = 0x00; for(count = 0x01;count <=TABLE_UP_TO_20 ;count++) { /* THis will print the table*/ printf(" %d * %d = %d\n", number,count, (number*count)); }
#include "stdio.h" int main() { unsigned int number, count; printf("Enter the Number \t"); scanf("%d", &number); printf("The even numbers are: \n"); for(count = 0x01; (count < number && number!= 0x00)) { if(count%2) { }else { printf("%d\n", count); } count++; } return 0; }
I would use a loop like this: const char *p= str-1; size_t count= 0; while (*++p) if (islower (*p)) ++count;
int count_whitespace (FILE* input) { int c, count=0; while (( c = getc(input) ) != EOF ) if ((char) c==' ') ++count; return count; }
Look at the "wc" command's man page, it will give you a count of all characters, including the newline character.
int i; for (i=1; i<=10; i++) printf ("%d %d\n", i, i*i);