C++ :: How To Find Repeated Words
Aug 3, 2014
I'm currently on an exercise that specifies that I find any repeated words, "the the" for example, in a string, print the word that is repeated and how many times said word is repeated using a while loop that reads one word at a time and break statements when a repeated word is found. Having a bit of a falling out with iterators tonight and I'm not doing to well.
View 7 Replies
ADVERTISEMENT
Oct 29, 2013
I need to find 2 similar words in one sentence and i need to find how much symbols are between these two words. I can't figure out the algorithm.
For example sentence would be-
This is nice table and nice chair.
Similar words are- nice
Symbols between them are 11 or 8(dont know if space is count as symbol)
int main()
char text[200],*dist;
printf("Enter one sentence
");
gets(text);
Maybe first of all would be
dist=strtok(text, " ,.!?");
while(dist!=0)
{
printf("%s
", dist);
dist=strtok(NULL, " ,.!?");
}
and it wil output each word and maybe then can search for similar words if there would be 2 strings then i would use strstr but i dont know how to do with one string.
View 1 Replies
View Related
Sep 23, 2013
Read in n, then n lastnames, and check to see if the first in the list is ever repeated again.Here's what I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
int strcmp(char *w1, char *w2);
int main() {
int j, n;
char string1[30], string2[30];
}
[code]....
I see the problem is that it lies within n amount of string that I am not going through every string to compare to the first one.How will do compare every string to the first one?
Current output:
3
alex
alex
alex
Not repeated
3
alex
ash
peter
Not repeated
View 2 Replies
View Related
Feb 10, 2014
I have to write a code that will read in n amount of names and the names. Then it will tell me if the first name entered has been repeated or not.
This is my input:
5 Bush Reagan Lincoln Bush Obama
This is my output:
First name in list is not repeated.
First name in list is not repeated.
First name in list is repeated.
First name in list is not repeated.
I would like to just give one answer; if it is repeated then the output should simply be "First name in list is repeated."
If the answer is that it was not repeated, I would like the output to say once "First name in list is not repeated."
// lastnames.c
#include <stdlib.h>
#include <stdio.h>
int main(void) {
char FIRST[25];
[code]....
View 2 Replies
View Related
Jan 26, 2015
I got a homework that require to count number of words in a text file and also display the first and last 10 words of the text file to the console. I have finished the counter problem and now I struggle showing the first and last 10 words.
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
int tokenize(string sentence, string tokenizedWords[]);
[code]....
View 2 Replies
View Related
Sep 13, 2013
I've been trying to do a RegEx, but I just can't seem to find the solution for it.
Do the following to find Test:
Test55: Will be allowed
TestABC: Will NOT be allowed
Basically, if Test is followed immediately by anything else other than a case-insensitive letter (not [a-zA-Z]), then it will pass.
View 3 Replies
View Related
Apr 3, 2015
I wrote this code to search words in the file and display if the program found the word or not. when i write the words in the file on the same line (without the endl) like this :
w_toFile << "gilbert";
w_toFile << "lara";
w_toFile << "rana";
i can search any word.
but when i write them with the endl, the program can find only the first word. what can i do to make the program find any word even if they are each on a line?
here is the full code:
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std;
int main (void) {
string wordsearch;
[Code] ....
View 5 Replies
View Related
Dec 20, 2013
I have written below program to count number of words and lines and print the all the words.
#include <iostream>
using namespace std;
#include<fstream>
#include<string.h>
int main() {
ofstream outfile;
[Code] .....
Its compiling fine but when executed its displaying I infinite times...
View 3 Replies
View Related
May 19, 2013
I was reading this earlier [URL] ..... and I was trying to figure out how to pick one of the words randomly from my text instead of using all the words in it.
View 4 Replies
View Related
Jul 12, 2013
I am using this code that to check a set of values from a text file and show it on the output.
Code: void MatchNumber(int b) {
vector<Rect> rects;
ifstream theFile("CheckNumber.txt");
double x1,y1,x2,y2;
while(theFile >> x1 >> y1 >> x2 >> y2 ){
rects.push_back(Rect(x1,y1, x2,y2));
}
int num=0;
[code]....
I want to calculate how many times the common number is repeated . So I have used freq[num] in that function. But I am getting the output like this-
Code:
The common number is = 5
The 5 repeated = 1 times
The common number is = 6
The 6 repeated = 1 times
The common number is = 4
The 4 repeated = 1 times
The common number is = 5
The 5 repeated = 1 times
[code]....
So the freq[num] is only returning 1 instead of counting the total number of repeating which is wrong!! I would like to have somthing like this in my output -
Code:
The common number is = 5
The common number is = 6
The common number is = 4
The common number is = 5
The common number is = 5
The common number is = 8
The common number is = 9
The common number is = 6
The common number is = 6
[code]....
How can I do that?
View 3 Replies
View Related
Oct 19, 2013
this code :
#include <cstdlib>
#include <iostream>
#define TRUE 1
#define FALSE 0
using namespace std;
typedef int Bool;
[Code] ....
Gives repeated digits in an integer but only in one condition : Only if the repeated digit is the result of n%10 where n is the integer the user writes. If the repeated digit is not the result of n%10 , then the compiler gives a wrong result.
so the question is : how to make this code gives the repeated digit in an integer (regardless the fact that the repeated digit is the result of n%10 or not and especially with making the minimum of changes on the code)????????? ?????
View 3 Replies
View Related
Mar 23, 2014
Printing duplicate 2D array elements
if for 1D array
for (int i = 0; i<0; i++){
for (int j = i+1; j<0; i++)
{
if (arra[i] == arra[j]){
cout<<arra[i]<<endl
}
}
}
how would i do for 2D
View 5 Replies
View Related
Apr 18, 2014
Given the matrix MxN, check if there are repeated numbers.
View 8 Replies
View Related
Jul 12, 2013
This function check a set of values from a text file and show it on the output.
void MatchNumber(int b){
vector<Rect> rects;
ifstream theFile("CheckNumber.txt");
[Code]......
I want to calculate how many times the number is repeated. So I have used freq[num] in that function. But I am getting the output like this-
The common number is = 5
The 5 repeated = 1 times
The common number is = 6
The 6 repeated = 1 times
The common number is = 4
The 4 repeated = 1 times
The common number is = 5
The 5 repeated = 1 times
The common number is = 5
The 5 repeated = 1 times
The common number is = 8
The 8 repeated = 1 times
The common number is = 9
The 9 repeated = 1 times
The common number is = 6
The 6 repeated = 1 times
The common number is = 6
The 6 repeated = 1 times
The common number is = 8
The 8 repeated = 1 times
So the value of freq[num] only shows 1 instead of calculating the number of repetition.
View 3 Replies
View Related
Mar 6, 2015
In the C++ book Programming: Principles and Practice Using C++, there's an example code like this for detecting repeated words:
Code:
#include "std_lib_facilities.h"
int main() {
int number_of_words = 0;
string previous = " "; // previous word; initialized to "not a word"
string current; // current word
while (cin >> current) // read a stream of words
[Code].....
The header file in there is sort of like a set of training-wheels for students who are complete beginners to the language; the function keep_window_open() is defined in there and it does just that on Windows systems where the output window closes too fast (in the case of the function, it's just like cin.ignore(), except it waits for you enter a character, like 'j', before it exits); programs on my Windows laptop work fine on Code::Blocks, but when I create a .exe file for them and double-click that file, it does actually close too quickly for me to be able to see the output (if it's a program like the generic "Hello World!" program that just outputs text to the screen and then exits - so all I see is the output window just flash-by really fast in those cases).
Anyway, as for the problem I'm having with the code: there are no error and compile- or link-time, but it does behave strangely at runtime, where the part inside the curly-braces of the while-loop doesn't execute at all.
View 3 Replies
View Related
Jan 30, 2015
So I already gave this a go and will post my code below. The question is about the last loop before the program cout's. i thought the count would keep track of the repeated numbers so i could display them but it does not. I italicized the loop i am referring to. or at least i tried to xD
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int inputs[30];
int numofloops;
int maxvalue = inputs[0];
[Code]...
View 1 Replies
View Related
Aug 26, 2014
I am working on a problem for the CS50 online course through the Harvard web site. I am trying to re-size a bmp image. They gave us a small program that copies a bmp image and we are to modify it to re-size the image. Here is the page [URL]..... Any way I am trying to move the file position indicator backwards so I can read the same line from the input file. So I can resize the file vertically I have the image resized horizontally but it keeps over writing the same lines vertically.
/**
* bmp.h
*
* Computer Science 50
* Problem Set 5
[Code]...
View 4 Replies
View Related
Aug 26, 2014
With out sorting and if the array size is changed and filled with different elements, mayb some that are all duplicates(in that case display message that says "No nonrepeated digits".
This seemed like a simple task but im failing to grasp some concept. if the array is 2,0,38,2,3,1,3 the first non repeated is 0.
nested loop is how i went. i feel like im going to far with the counters than need be. just need first non repeated. if n equals 2 an o equals 0. and it iterates comparing 0,38,2,3,1,3 to n which is 2. repCount will b 1. if repcount was 0 then n would be the first non repeated number. im trying to consolidate my if else statements so there are not so many. This program fails if the array gets bigger say 2,45,2,7,1,8,9,45,1,10 .....
for (n=0; n<sizeof(digits)/sizeof(int); n++) {
if(repCount<2) {
// nonRepCount=0;
for (o=n+1; o<sizeof(digits)/sizeof(int); o++)
[Code] .....
View 1 Replies
View Related
Mar 20, 2014
I have generated a 10x10 integer matrix, by way of a 2 dimensional array, the elements are randomly generated and lie on 1 <= z <= 5. I am in need of an efficient method of setting all adjacent "duplicates" (repeating elements) of length 3 or greater to the integer six (6). The source for the brute method follows for clarity.
Code:
46 for(row=0;row<10;row++)
47 {
48 for(col=0;col<10;col++)
49 {
50 board[row][col]=rand()%4+1; //filling matrix here
[Code] .....
I know there must exist a much more elegant approach than listing out all permutations.
View 1 Replies
View Related
Jan 27, 2014
I found a card shuffling function on a long dead thread.
Code:
for (int x = 52; x > 0 ; x--)
{ y = rand() % x;
temp = deck[x];
deck[x] = deck[y];
deck[y] = temp; }
I do not understand how numbers are not repeated in the function. It seems like it would be possible to get 2 cards with the same number with the above function.
I also am getting incorrect numbers. It seems like I should only get numbers between 1 and 52 (which is what I want). However, I am getting the number 0, and some number between 1 and 52 will be missing, but I will have a total of 52 unique numbers.
I played around with using 51 instead of 52, x > 1, and changed to --x and none of those produced the desired results.
View 5 Replies
View Related
Jul 30, 2014
I have a text file with repeated lines, and i would like to get rid of the duplicate information, an algorithm to achieve this?
Example of my text file (the actual file may be huge, up to 500MB):
+Hello
+Bye
*The sun
-The moon
+Bye
/One coin
+Bye
+Bye
+Hello
*The sun
And i would expect to get something like this:
+Hello
+Bye
*The sun
-The moon
/One coin
I know how to open and read a file with fstream and getline(), but i don't know how to make the comparison.
View 6 Replies
View Related
Apr 24, 2014
I have several functions doing similar things, inside their implementations, the parameters are the same, but they call different methods.
I want to create one function to make the structure easier, and reduce the duplication. I heard template might be one solution. But I am not sure how to use it in this case.
void GetA(...XXX) {
for()
{
[Code].....
View 1 Replies
View Related
Dec 20, 2014
I'm working on a game with an entity-component system. I have a Manager class that is handling all of the components by feeding them into vectors set up for each type of component. Right now I have a lot of repeated code for each kind of component.
void add_pos_comp(shared_ptr<Position> pos_comp);
shared_ptr<Position> get_pos_comp(int id);
const vector<shared_ptr<Position>>& get_pos_comps();
void add_vel_comp(shared_ptr<Velocity> vel_comp);
shared_ptr<Velocity> get_vel_comp(int id);
const vector<shared_ptr<Velocity>>& get_vel_comps();
I've tried generalizing this with templates, but I've been confused by how I should organize the collection of vectors for each component. I had it setup so that they were all in an unordered_map, but how to get the templated functions to find the right map slot to use based simply on the template's parameters.
The full project is here for reference: [URL] ....
View 2 Replies
View Related
Sep 26, 2014
for (Initializing; Boolean_Expression; Update) i'm tying to use the words instead of numbers to repeats ,already i changed( Initializing; Boolean_Expression and Update) it doesn't work ,all i got is just Numbers !!
# include <stdio.h>
main()
{
[Code]....
View 3 Replies
View Related
Mar 6, 2015
I am creating a trie (not a tree but a trie). I have the following structure:
Code:
struct tnode {
bool is_word;
list *children;
};
struct trie {
int length;
struct tnode *first;
};
list *children is a linked list that contain pointers to tnodes. For example: I have a trie with the words cup and cut:
c
u
/
p t
Given the example above you have the following linked lists:
1. [c] that contains a pointer to linked list 2.
2. [u] that contains a pointer to linked list 3.
3. [p, t]
Now I want to print the trie like:
cut
cup
View 2 Replies
View Related
Oct 2, 2014
I am trying to write a program in C to reverse a string .
Example - This is America
output - America is This
I have thought of two ways of doing this question
1) using 2D arrays ( no pointer)
2) Using Strings and pointer
Code:
#include<stdio.h>
#include<string.h>
main()
}
[code]....
View 4 Replies
View Related