C++ :: Program That Can Count Space?
Jun 8, 2013I was coding a program that can count space, new_line, and other characters in a paragraph, but it doesn't work.
#include <stdio.h>
int main(void)
{
[Code].....
I was coding a program that can count space, new_line, and other characters in a paragraph, but it doesn't work.
#include <stdio.h>
int main(void)
{
[Code].....
4.1 Write a program that will count from 1 to 12 and print the count, and its square, for each count.
4.2 Write a program that counts from 1 to 12 and prints the count and its inversion to 5 decimal places for each count. This will require a floating point number.
4.3 Write a program that will count from 1 to 100 and print only those values between 32 and 39, one to a line. Use the incrementing operator for this program.
I am trying to make my program read an integer until a space, namely ' ', is pressed, and then move on to the next command. However, all my attempts hitherto with both scanf() and getchar() have been to no avail. Every time it proceeds to the next command only upon pressing Enter, which is not quite what I want.
View 3 Replies View RelatedI'm creating a space simulator program for uni and I've got 3 errors that I just can't seem to fix.
View 10 Replies View RelatedQuestion is : Write a program that opens a file and counts the whitespace-separated words in that file.?
my answer is :
#include <iostream>
using namespace std;
#include <iostream>
#include <string>
#include <fstream>
int main() {
string filename = "Question 1.cpp"; // File name goes in here
[Code] ....
Now I am not sure if im suppose to get a msg saying : The file "Question 1.cpp" has 0 words.
im wondering is the question that im being asked, asking me to input a string of words and then the compiler when it builds and runs the program counts the word spaces.?
Write a C++ program that reads lines of text from a file using the ifstream getline() method, tokenizes the lines into words ("tokens") using strtok(), and keeps statistics on the data in the file. Your input and output file names will be supplied to your program on the command line, which you will access using argc and argv[].
You need to count the total number of words, the number of unique words, the count of each individual word, and the number of lines. Also, remember and print the longest and shortest words in the file. If there is a tie for longest or shortest word, you may resolve the tie in any consistent manner (e.g., use either the first one or the last one found, but use the same method for both longest and shortest).
You may assume the lines comprise words (contiguous lower-case letters [a-z]) separated by spaces, terminated with a period. You may ignore the possibility of other punctuation marks, including possessives or contractions, like in "Jim's house". Lines before the last one in the file will have a newline (' ') after the period. In your data files, omit the ' ' on the last line. You may assume that the lines will be no longer than 100 characters, the individual words will be no longer than 15 letters and there will be no more than 100 unique words in the file.
Read the lines from the input file, and echo-print them to the output file. After reaching end-of-file on the input file (or reading a line of length zero, which you should treat as the end of the input data), print the words with their occurrence counts, one word/count pair per line, and the collected statistics to the output file. You will also need to create other test files of your own. Also, your program must work correctly with an EMPTY input file – which has NO statistics.
Test file looks like this (exactly 4 lines, with NO NEWLINE on the last line):
the quick brown fox jumps over the lazy dog.
now is the time for all good men to come to the aid of their party.
all i want for christmas is my two front teeth.
the quick brown fox jumps over a lazy dog.
Copy and paste this into a small file for one of your tests.
Hints: Use a 2-dimensional array of char, 100 rows by 16 columns (why not 15?), to hold the unique words, and a 1-dimensional array of ints with 100 elements to hold the associated counts. For each word, scan through the occupied lines in the array for a match (use strcmp()), and if you find a match, increment the associated count, otherwise (you got past the last word), add the word to the table and set its count to 1.
The separate longest word and the shortest word need to be saved off in their own C-strings. (Why can't you just keep a pointer to them in the tokenized data?)
Remember – put NO NEWLINE at the end of the last line, or your test for end-of-file might not work correctly. (This may cause the program to read a zero-length line before seeing end-of-file.)
Here is my solution:
#include<iostream>
#include<iomanip>
#include<fstream>
using std::cout;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::cin;
using std::getline;
void totalwordCount(ifstream&, ofstream&);
[Code] .....
Question: In the uniquewordCount() function, I am having trouble counting the total number of unique words and counting the number of occurrences of each word. In the shortestWord() and longestWord() function, I am having trouble printing the longest and shortest word in the file. In the countLines() function, I think I got that function correct, but it is not printing the total number of lines. Is there anything that I need to fix in those functions?
My goal is to create a program that reads a string and counts how many Uppercase, Lowercase, Spaces, and digits there are in the string. Right now this the output i get.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
int iochar;
char string;
[Code] .....
How we will write a program that will count a number of notes. I mean if i have 5676 rupees and i want to find the number of 5 thousand pak currency ,the number of 1000 notes, the number of 500 notes and the number of 100 notes. How we design such a program of if rlse structure to perform the above task.
View 1 Replies View Related#include <stdio.h>
#include <string.h>
int main() {
char string[100];
int c= 0, count[26] = {0};
printf("Enter a string: ", string);
[Code]...
This is the output I receive:
I need counting the Uppercase Letters.
My program does compile and counts the number of lines of code, which is LOC: 20. My issue this, according to my code instruction it should not count comments (//) and also shouldn't count blank lines of code. Unfortunately my code is counting // and white space.
What seems to be working is #, all three includes are not being counted. That’s a good sign. I’m using the same technique not to count #. Why isn't working for comments and blank lines? I’m pretty sure that my logic is correct. I think my problem is maybe syntax.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
//read my file
[code]....
I'm writing a program that reads and counts all the printable characters (ASCII 32-126)found in a text file.
For example if the text file read: Why so serious?
The output to the screen would display(in order of ascii value however):
Character-----Total
W--------------1
h--------------1
y--------------1
s--------------3
o--------------2
etc...
However, I don't think it's reading any of the characters.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main() {
int i, count[127];
[Code] .....
I am new in c programming. I want to write a function that will count the empty line of the file and display the content.
/* ***************************
** creator: firstprint *
******************************/
#include <stdio.h>
/* function that will identify if line is empty or not */
is_empty(char *buffer) {
while(isspace(*buffer)) buffer++;
if(*buffer==0x00) return 1;
[Code] ....
I have a .txt file that contains, together with a few characters, columns of values that I want to save in different files like is written in the program (file 1, file2, file3 - a with x, b with y, c with z). The pattern of the source text file is like this:
known_vector frame1 151 65 0 frame2 151.000763 64.538582 0.563737
known_vector frame1 152 65 0 frame2 152.000702 64.542488 0.560822
known_vector frame1 153 65 0 frame2 153.000671 64.546150 0.558089
Until now I could manage to split the files, but the output gives me only zeros. First the program count the number of lines of the read text file, then it should display the desired columns of double values in three other .txt files.I've got for the three .txt files columns like this:
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
I'm making some progress. My program does compile and output the number of line per code, but it shouldn't count comments and blank lines. I tried using (s.substr(0,2) == "//") as suggested but it didn't work.
This is my improved code:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int A = 3;
int main() {
string s;
[code].....
C program to count the number of lines in a text file and reverse the contents in the file to write in an output file.
ex :
input
one
two
three
output:
three
two
one
I tested my count funtion. So my count function is not working properly, it should return 5 because 5 words have prefix "tal," but it is giving me 10. It's counting blank nodes.
This is my main.cpp file
int main() {
string word;
cout<<"Enter a word"<<endl;
cin >> word;
string filename;
[Code] .....
Here is what i have so far:
#include<fstream>
#include<iostream>
#include<string>
[Code].....
I also need to do a loop that scan the count array and check if the element is bigger than the previous one.
Code:
#include <stdio.h>
main() {
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '')
++n1;
printf("%d", n1);
}
I have a more complicated program I'm wishing to have display the output, however, to save some time I'm using an example of a shorter version. count the lines in input and display the output in terminal when ./program is executed after compilation. To count and compute lines, words and within arrays.
how to input with space what i mean is like this, for example:
Product Code: 0000001
Product Name: Mobile Phone
if the users input is like that in the (Product Name)it will skip the next input that is the
Price: Quantity: 100
Manufacturer: Alcatel
something like that...
Heres the code
Code:
if(b<=10)
{/*Start for IF Statement*/
for(a=1;a<=b;a++)
{/*Start of FOR loop*/
printf("
");
printf("Product Code : ");
scanf("%s", &prod_code);
}
[code]....
I used pointer(or is it not?) to make it one part only alphabets and the other one digits. The coding, calculate_charges.c and the open file, customer.txt are attached at the end of the post.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 3
void trimback(char input[], int strnameindex);
void trimfrnt(char input[], int strnameindex);
}
[code]....
What would the worst, average and best case space complexity be for a data structure of type map<string, vector<int> > in big O notation? I'm parsing through a document and storing each word as a key and im attaching an associated int (in a vector) to it as the value.
View 4 Replies View RelatedCan we use using declaration within name space scope? is it safe to use it?
I think we can use using declaration for class scope and function scope only.
I want the user to be able to enter a command then a character, like for example: push r....I want the command to be stored in the array command, and the character to be stored in the variable c.
Now I wonder what the best way to get rid of the space is, using scanf or getchar (see below for code, only thing that I changed between the 2 versions is the statement before the comment "get rid of space")? Or maybe it doesnt matter?
Code:
include <stdio.h>
#define MAX 200
void push(char c); // Puts a new element last in queue
char pop(void); // Gets the first element in queue
static char s[MAX];
}
[code]....
I am allocating space only for two characters but it fits all of them, if you run this it will print the whole string literal "hello my friend". How is that possible?
I am using gcc 4.6.3., I know about strncpy().
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char* str = new char[2];
strcpy(str, "hello my friend");
cout << str << endl;
return 0;
}
how can I read some strings that contains spaces and put them in a vector of strings, using the push_back() function?
I have a collection of functions, for example: [multiply_by_forty two, add_by_five]. All I want to do is to store the strings like: multiply_by, add_by in a vector of strings, and the arguments:forty two, five etc in another vector of strings, but with spaces. The function convert() converts written numbers to numbers (for ex the output of covert("forty two")is 42;)
Here is the code:
public:void split() {
vector < string > s1;
vector < string > s2;
[Code].....
In my code I have a template class.
In the contructor, I create a new array arrray.
A pointer to this array is saved as an attribute.
The purpose of this attribute is that I should be able to call the array from within another function be typing *p or *(p+1) etc.
The only thing I forgot is that the array doesn't exist anymore when the constructor is done, right?
This means the space is no longer reserved for this array and the space is used for other things.
Is there a way to keep the array space reserved (even when the array does not exist anymore at the end of the function)?