code is just freezing and not outputting anything - c++

so i have an input.txt file that contains english-german words
for example book-buch, area-Bereich etc (those "-" are also included)
i have to code a small translator so if input book the code has to output buch and vise versa
i already coded the english to german output but for some reason when i try the same logic on german to english it doesnt output anything
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main() {
ifstream ifs("input.txt");
ofstream ofs("history.txt", ifstream::app);
int a, b, c = 0, j;
string m, l, p[100];
for (a = 0; a < 50; a++) {
ifs >> p[a];
m = p[a];
}
cin >> l;
for (a = 0; a < 50; a++) {
if (l == p[a].substr(l.size() + 1, p[a].size())) { //here's the problem
cout << p[a].substr(0, p[a].size() - l.size() + 1);
}
if (l == p[a].substr(0, l.size())) { //english to german
cout << p[a].substr(l.size() + 1, p[a].size());
ofs << l << "-" << p[a].substr(l.size() + 1, p[a].size());
ofs << endl;
}
}

i used a different method
for (a = 0; a < 50; a++) {
pos = p[a].find("-");
if (l == p[a].substr(pos + 1)) {
pos = p[a].find("-");
cout << p[a].substr(0, pos);
also somehow forgot that c++ compiler was case sensitive
silly me

Related

My Boyer-Moore algorithm only searches the first 3000ish characters in my text file

I'm trying to implement a Boyer-Moore string search algorithm. The search algorithm itself seems to work fine, up until a point. It prints out all occurrences until it reaches around the 3300 character area, then it does not search any further.
I am unsure if this is to do with the text file being too big to fit into my string or something entirely different. When I try and print the string holding the text file, it cuts off the first 185122 characters as well. For reference, the text file is Lord of the Rings: Fellowship of the Ring - it is 1016844 characters long.
Here is my code for reference:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <chrono>
using namespace std;
# define number_chars 256
typedef std::chrono::steady_clock clocktime;
void boyer_moore(string text, string pattern, int textlength, int patlength) {
clocktime::time_point start = clocktime::now();
vector<int> indexes;
int chars[number_chars];
for (int i = 0; i < number_chars; i++) {
chars[i] = -1;
}
for (int i = 0; i < patlength; i++) {
chars[(int)pattern[i]] = i;
}
int shift = 0;
while (shift <= (textlength - patlength)) {
int j = patlength - 1;
while (j >= 0 && pattern[j] == text[shift + j]) {
j--;
}
if (j < 0) {
indexes.push_back(shift);
if (shift + patlength < textlength) {
shift += patlength - chars[text[shift + patlength]];
}
else {
shift += 1;
}
}
else {
shift += max(1, j - chars[text[shift + j]]);
}
}
clocktime::time_point end = clocktime::now();
auto time_taken = chrono::duration_cast<chrono::milliseconds>(end - start).count();
for (int in : indexes) {
cout << in << endl;
}
}
int main() {
ifstream myFile;
//https://www.kaggle.com/ashishsinhaiitr/lord-of-the-rings-text/version/1#01%20-%20The%20Fellowship%20Of%20The%20Ring.txt
myFile.open("lotr.txt");
if (!myFile) {
cout << "no text file found";
}
string text((istreambuf_iterator<char>(myFile)), (istreambuf_iterator<char>()));
cout << text;
string pattern;
cin >> pattern;
int n = text.size();
int m = pattern.size();
boyer_moore(text, pattern, n, m);
}
I have tried to do some researching about what could be the cause but couldn't find anyone with this particular issue. Would appreciate any nudges in the right direction.

Simple binary search doesn't work for me. I am trying to search for a word from text file

I built a program in C++ that takes words from txt file and inputs into program. Program then stores these words into array. Now, I want to search a specific word among the array using binary search.
My txt file has the following words:
hello
world
hi
how
are
you
i
am
fine
thank
welcome
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
int binarySearch(string words[], const string& x,int n)
{
int l = 0 ;
int r = n - 1;
while (l <= r)
{
int m = l + (r - l) / 2;
int res;
if (x == (words[m]))
res = 0;
// Check if x is present at mid
if (res == 0)
return m;
// If x greater, ignore left half
if (x > (words[m]))
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
return -1;
}
int main () {
ifstream inFile;
inFile.open("test.txt");
if(inFile.fail()){
cerr << "Error opening file"<< endl ;
exit(1);
}
string x1;
string words[100];
int count=0,i=0;
string str;
while( !inFile.eof()) {
inFile >> x1;
words[i]=x1;
count++;
i++;
}
for (i=0;i<100;i++){
cout<< words[i]<<endl;
}
string x;
x = "how";
int n = 14;
int result = binarySearch(words , x,n);
if (result == -1)
cout << ("\nElement not present");
else
cout << ("Element found at index ") << result;
return 0;
}
I can't find the words except Hello which is the first word. So please help me.
hopefully this work
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
int binarySearch(string words[], const string& x, int n)
{
int l = 0;
int r = n - 1;
while (l <= r)
{
int m = l + (r - l) / 2;
int res = 0;
if (x == (words[m]))
res = 0;
// Check if x is present at mid
if (res == 0)
return m;
// If x greater, ignore left half
if (x > (words[m]))
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
return -1;
}
int main() {
ifstream inFile;
inFile.open("test.txt");
if (inFile.fail()) {
cerr << "Error opening file" << endl;
exit(1);
}
string x1;
string words[100];
int count = 0, i = 0;
string str;
while (!inFile.eof()) {
inFile >> x1;
words[i] = x1;
count++;
i++;
}
for (i = 0; i < 100; i++) {
cout << words[i] << endl;
}
string x;
x = "fine";
int n = 11;
int result = binarySearch(words, x, n);
if (result == -1)
cout << ("\nElement not present");
else
cout << ("Element found at index ") << result;
return 0;
}

Getting out of a while loop earlier with cout using c++

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
string sentence = "some random sentence";
int i = 0; //runs through the bigger string
int j = 0; //runs through the smaller string
int k = 0; //variable to mark the position where the string starts being equal in order to delete it using substring
string remove = "random";
int a = sentence.size();
int b = remove.size();
while (i < a)
{
if (sentence[i] == remove[j])
{
if (b == j - 1)
{
cout << sentence.substr(0, k) << sentence.substr(i, (a - 1));
break;
}
else
{
i++;
j++;
}
}
else
{
i++;
j = 0;
k++;
}
}
return 1;
}
I want to remove the word random from the bigger string and print it out but when I run the code, it does not return anything. What's missing?
I already tried putting a break right below de "cout", but it does not work.
Thank you :)
As b == 6, j would have to be 7 in order for b == j-1 to become true. But remove[6] is the terminating \0 of the random string, so j can never grow beyond 6.
Here is the code I edited
if (b-1 == j)
{
cout << sentence.substr(0, k) << sentence.substr(i+2, (a - 1));
break;
}
This is assuming, you have spaces between the words.

Converting call from a .txt into a string c++

I'm trying to read from a text file and then count the number of occurences of each word and then export the results to a different text file. I'm only allowed to use loops and arrays for this assignment. I'm just looking for a slight push in the right direction, primarily in the beginning of the code. It's not compiling correctly!
using namespace std;
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
void put(string word, string words[], int counts[], int & size)
{
int w;
for (w = 0; w < size && word >= words[w]; w++);
if (w >= size) {
words[w] = word;
counts[w] = 1;
++size;
}
else if (word < words[w]) {
for (int i = size - 1; i >= w; i--) {
words[i + 1] = words[i];
counts[i + 1] = counts[i];
}
words[w] = word;
counts[w] = 1;
++size;
}
else {
counts[w] = counts[w] + 1;
}
}
int main()
{
int word;
ifstream input("input.txt");
ofstream chout("charcount.txt");
ofstream wout("wordscounts.txt");
int inputSize = sizeof(input) / sizeof(string);
int counts[100];
const int MAX = 100;
string words[MAX];
int wordsSize = 0;
while (input >> word) {
put(input[word], words, counts, wordsSize);
}
wout << " Word Frequency" << endl;
for (word = 0; word < inputSize; ++word) {
wout << setw(10) << words[word] << setw(4) << counts[word] << endl;
}
chout.close();
wout.close();
system("pause");
return 0;
}
This line:
put(input[word], words, counts, wordsSize);
You are trying to index into an ifstream. This is simply not allowed. The compiler (g++ 4.8.4) said:
no match for ‘operator[]’ (operand types are ‘std::ifstream {aka std::basic_ifstream<char>}’ and ‘int’)

How to count occurrences of individual letters

Task: Write a program that will read a line of text and output the number of occurrences each letter.
#include <iostream>
#include <string>
#include <cstring>
#define N 100
using namespace std;
int main()
{
char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
int alphacount[26];
char lot[N], *p1;
int txtlen, *p2;
cout << " Enter a line of text: " << endl;
cin.getline(lot, 99);
txtlen = strlen(lot);
p1 = lot;
p2 = &txtlen;
for (int x = 0; x < *p2; x++)
{
for (int y = 0; y < 26; y++)
{
if (*p1 == alphabet[y])
{
alphacount[y]++;
p1++;
}
}
}
cout <<;
}
What is the condition needed, and what variable will be used to output the occurrences of letter? For example:
> enter a line of text : mervlala
Output:
a - 2,
e - 1,
l - 2,
m - 1,
r - 1,
v - 1
You are programming on c++, you should use the c++ way.
#include <algorithm>
int main ()
{
std::string textline;
std::getline (std::cin,textline);
for(char ch = 'a'; ch <= 'z'; ch++)
{
std::size_t n = std::count(textline.begin(), textline.end(), ch);
if(n > 0)
std::cout << " - " << n << "," << std::endl;
}
}