C++ :: Counting Characters / Words / Lines And Digits
Mar 20, 2013
I am writing a program for my class in C++. For this program we are required to use different fuctions and prototypes outside of main. We have to determine the number of characters, lines, sentences, digits, words, etc. in a particular function the user types in.
I have to write a program (on linux) which will count character, words and lines like wc linux command. I'm trying to write this for last 3 days... First part of app I did and it works fine - command line options to choose. Then I've got a function read_file which I have to use to read a file. One of the options is to get the file name from user and if user will not type any name then the standard file is ubuntu dict file /usr/share/dict/words, this is not working as well...
Counting characters and lines is working fine but because I don't know how to get text from read_file wrote code to read file interior this functions. Words counting is working partly - everything is fine until there are two or more spaces, tabs one after another then counts extra words. Finally I need child processes in words and lines counting functions. Parent process should waits for all childs to finish and should be pipes to submit character counts back to parent process. How to do all this things with processes...
Code:
#include <getopt.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <errno.h> /*size of character buffer to read in a file. */ #define BUFFSIZE 1000000
I'm doing an exercise that prints all input lines that are longer than 80 characters. I rather not use any libraries so I decided to write my own function that counts characters to use it in my main program. However when integrate things my function returns zero all the time.
Here is my full code:
/* Exercise 1-17 Write a program to print all input lines that are longer than 80 characters */
#include<stdio.h> /* Declarations*/ #define MAX_STRING_LEN 1000 int count_characters(char S1[]); int main() {
[Code] .....
So I was trying to debug my count_characters() function and this is the code if I was to run it seperately:
Code: #include <stdio.h> /* counts character of a string*/ main() { int nc = 0; int c; for (nc = 0; (c = getchar()) != ' '; ++nc); printf("Number of characters = %d ", nc); }
How I would go about counting lines from a file at the same time as extracting words? I need to know the line number to output what line the word is misspelled on. I tried getline and using sstreams but I could not get it to work.
it looks like a popular method for determining the total lines in a file is to read the entire file character by character in search of ' '. I have a file with 5 lines, but for some reason this code isn't finding any instances of ' '. Is this possible? Is there a better way to get the number of lines in a file?
The file looks like this:
Code: NAME: John FRIEND 1: Steve FRIEND 2: Andrea FRIEND 3: Ken OCCUPATION: Programmer
I was trying to make a function that would return how many lines are in a certain file. I was looking at [URL] which talks about how to count how many characters are in a text file. I just want the number of lines with in a file.
This code is compile but must have some upgrades :
1. The algorithm count double space how 1 word this is not correct. Probably have to add condition like ' '&'a-z'||'A-Z' but in unknown to me reason this is don't work
2. If there is one words without space then counted don't detect this word, it is incorrect.
Code: #include <iostream> using namespace std; int main () { char ch; cout <<"enter your text: " ;
I am writing a program when the beginning it you have to pick which step to preform out of a few steps. Once you choose the step. The user is asked to input what is asked and it should preform it. I have completed most of my steps but the one step giving me a hard time is converting a user inputed sentence into all lowercase letters, and then count the words of it. My program starts out like this if you pick this step Code: else
if (choice == 3) { /* Gather user's input */ printf(" You have chosen to convert a sentence to all lowercase letters and count the words " "Enter the sentence: "); gets ( sentence );
Call the function to convert the whole sentence to lowercase and count the number of words */
lowercase_string ( &sentence); countSentence ( &sentence); } /* End if statement */
this is the easy part in which I presume I did correctly, but now this is where I am getting the problems:
/* Prompts user to enter a sentence and convert it to lowercase and count the number of words */
I want to output the original sentence, then the sentence with all lower case letters, then at last output the number of words in the sentence. I declared all my variables up top, and I am wondering where my mistake is. When trying to compile it, I only get syntax errors which I don't understand.
I need to count Words in a string in <stdio.h> and <string.h> format ONLY, must be in WHILE Loop! Our professor acts like were programming students when were just marketing students
I'm trying to read files, count words in the files and go in directories. The problem is how I return numbers of total words and total directories. When it comes to nftw(), I am little confused.
I am trying to write a program that counts specific words that a user inputs "Howdy/howdy/Whoop/whoop" (yes I go to Texas A&M, hence those specific words) I am having an issue where it wont count the first word even if it is "Howdy"
For example if I put it:
"Howdy howdy whoop Whoop" - it only outputs that it counted 3 words
now if I were to do:
"Hello Howdy howdy whoop Whoop" - it would count 4.
Code:
#include "std_lib_facilities_4.h" int main(){ cout << "Please enter desired words, when you have entered all words, please type CTRL+d (EOF Command) " << endl; //It was assumed that EOF command was going to be used here hence the necessity of 'CTRL+d' vector<string>words;
So I'm trying to count the number of lines in a text file that is inputted by the user. Also the code doesn't sum up the last number of the text file (exmp it calculates and print only 14 from 15 numbers). I'm a beginner in c++ programing.
I'm creating a program to read information about class schedules at my school, reformat the information, and allow the user to search for specific semesters. There are eight fields of information. I'm reading the info from a text file using eight parallel arrays, but I'm having trouble declaring the arrays. I can run this code in one compiler (Dev-C++) with no problems, but I get errors when trying to compile it using Visual Studio stating that arrays must be declared with a constant value. I have a loop to run through the text file, with a counter to increment with each subsequent line, then I create a constant int equal to the counter, and declare the arrays of size equal to the constant int. Here's the section of code in question:
// Counting the number of lines in the text file inFileForLines.open("CIS225HW1DA.txt"); string countLine; int numberOfLines = 0; //Discarding the first line of the text file containing only column headings getline(inFileForLines, countLine);
Write a program that prompts the user for the name of a file. Then it opens the file, and counts the number of words and lines in the file, and prints out those counts.
I think I possibly could somehow use a counter to increment using getLIne() until getLine() returns NULL, but the problem is, I'm worried if I try that, a file that looks like this:
Bla bla bla bla bla lkfdljkfaklafdskjladsjkdfkjlkdfjdfshafdsjkjrerjkkjfaddjkfsafkjdjakdfsjkasfjkjkfdskjldfjkfjkdjfkdsakdjfkjfdkjdfskjfdsk jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjdfkerea blkjadkjlfdskjldfkjlfdkjfdjkdfsjkldfskljfksfdljfd
Only register four lines and not get the one after the fourth line.
I have to code a simple program who determining the number of Characters (A character could be any alphabets, digits, punctuation marks, or special , Operators ( Operators are those symbols that are used in mathematica expression, such as,'+', '*', '/', '-', and so on.), Uppercase letters (Uppercase characters are those from A..Z) and Numerical digits ( A digit is any of the Hindu-Arabic numerals from 0..9). Why the output is wrong!
#include <iostream> #include <string> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std ; int main() { char text;
[Code] .....
This is my input file This is a possible factorial function in a programming language called LISP
(defun factorial (n) (if (< n 2) 1 (* n (factorial (1- n)))))
This is my output:
The number of characters = 113 The number of operators = 3 The number of numerical digits = 3 Uppercase letters = 5
I think that "characters" is wrong, but I do not know why !
I have recently hit a stump with C++ and have been getting pretty frustrated with this assignment. I just can't seem to find out how to start the assignment. Basically for the first part of the assignment, I need to find the number of characters in this .txt file my teacher provided for me. The only exception is that I can only count the actual letters themselves, not spaces or punctuation. How would I go about doing this? I have read about functions like isalpha but can't figure out on how to fit them into code to do the first part of this assignment.
So I've done this program that's supposed to count how many vowels or consonants you enter, depending on which one you choose, and also count the total number of characters you've entered. But it's not outputting the correct answers. The current output that it's showing seem to me that it's going out of bounds or something.
#include <iostream> #include <cctype> using namespace std;
#include "stdafx.h" #include "string.h" #include "ctype.h" /*selection sort*/ void swap(int A[], int j, int k) { int p = A[k];
[Code] ....
Theoretically it should cound the number of apperances of each character in a string, but:
1) It is counting characters from letter "j" upwards thus all asci characters below "j" are not counted, why is that?
2) I want also to make this code calculate the number of apperances of each character in a txt file. How to do that, i know i have to open a file in read mode, that's all (I am new to c programming)?
So I need to make a program that counts the number of words, lines, and vowels in a program. Punctuation and white spaces are not counted in any of the counts. I have this so far and the words, and lines work and are correct but I can't seem to get the vowel count to work.
I'm trying to copy a file into another file and copy the number of characters copied but my while loop doesn't even enter into a loop indicating the file is already at the EOF file character. I've confirmed this by placing the printf() statement inside the while loop, which doesn't print anything and by keeping it out of the while loop and changing the chars_copied to something like 9, it prints 9 for number of chars_copied. I don't understand why the file is already at the EOF character, I've tried this with a few more files, it's the same result.
Code: #include <stdio.h> int main(void){ FILE *input_file, *output_file; int c, chars_copied=0;
if((input_file=fopen("C:workmarks.txt", "r"))==NULL) perror("input file open failed");
So i have this program that's supposed to count all the characters including the spaces without using strlen but it only counts the number of the first word's characters and it does not include the rest of the words after the first space. how do i include spaces in the count?
I want the code to search for the first occurence of the word "character," and start counting the lines from that line until it hit the first occurrence of the word "story."
Right now, I am only getting a counter value of 1.