The task is to delete all repeating words from the string and then print the addresses of these words (meaning the addresses of the first letters) in the original string on the screen.
My code looks like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string inputString = "cactus word programmer cat word dog cat. cat mug words.";
inputString += ' ';
string copiedString = inputString;
string word;
int wordSize = 0;
int startIndex = 0;
for (size_t i = 0; i < copiedString.size(); i++)
{
if (copiedString[i] == ' ' || copiedString[i] == '.')
{
//startIndex = i;
word.insert(0, copiedString, startIndex, wordSize);
while (copiedString.find(' ' + word + ' ', startIndex + wordSize) != -1 ||
copiedString.find(' ' + word + '.', startIndex + wordSize) != -1)
{
string *address = (inputString.find(word, startIndex + wordSize) + &inputString);
cout << *address << '\t' << address << endl;
copiedString.erase(copiedString.find(word, startIndex + wordSize), wordSize + 1);
}
while (copiedString.find(' ' + ' ') != -1)
{
copiedString.erase(copiedString.find(' ' + ' ', 1));
}
word.clear();
wordSize = 0;
startIndex = i + 1;
}
else
wordSize++;
}
cout << inputString << endl;
cout << copiedString << endl;
return 0;
}
So I tried to make it work with both dots and spaces. For that I needed a loop which deletes all
unnecessary spaces (the second while loop). The program doesn't delete spaces, however, and throws some exceptions saying I'm trying to access wrong part of the memory (I think) and it prints incorrect addresses (last 2 repeat for some reason).
Ended up rewriting the whole thing but this is what I came up with:
int main()
{
string inputString = "cactus. word programmer cat word dog cat. cat. cat mug words. words. abc abcde abba programmer";
inputString = inputString + ' ';
string copiedString = inputString;
string word;
int wordSize = 0;
int startIndex = 0;
for (size_t i = 0; i < copiedString.size(); i++)
{
if (copiedString[i] == ' ' || copiedString[i] == '.')
{
word.insert(0, copiedString, startIndex, wordSize);
int k = 0;
while (copiedString.find(' ' + word + ' ', startIndex + wordSize) != -1)
{
const size_t currentIndex = inputString.find(' ' + word + ' ', startIndex + wordSize);
cout << word;
if (wordSize >= 7)
cout << '\t';
else
cout << "\t\t";
cout << (inputString.find(' ' + word + ' ', currentIndex + k * wordSize) + &inputString) << endl;
copiedString.erase(copiedString.find(' ' + word + ' ', startIndex + wordSize), wordSize + 1);
k++;
}
k = 0;
while (copiedString.find(' ' + word + '.', startIndex + wordSize) != -1)
{
const size_t currentIndex = inputString.find(' ' + word + '.', startIndex + wordSize);
cout << word;
if (wordSize >= 7)
cout << '\t';
else
cout << "\t\t";
cout << (inputString.find(' ' + word + '.', currentIndex + k * wordSize) + &inputString) << endl;
copiedString.erase(copiedString.find(' ' + word + '.', startIndex + wordSize), wordSize + 2);
k++;
}
word.clear();
wordSize = 0;
startIndex = i + 1;
}
else
wordSize++;
}
cout << endl << inputString << endl;
cout << copiedString << endl;
return 0;
}
Related
I am working on a college project and this is a railway ticketing system I developed. Now, the problem is that the data is being saved on the files created but when I want to read that data from the files nothing shows up in the console window. I have tried altering it many times but the problem stays there. I can't seem to find a solution to this, it would be great if you guys could help.
else if (choice == 4)
{
char b[13];
cout << "\n\nPress 1 to display Rawalpindi to Lahore Queue";
cout << "\nPress 2 to display Lahore to Karachi Queue";
cout << "\nPress 3 to display Rawalpindi to Karachi Queue" << endl;
cin >> fchoice;
if (fchoice == 1)
{
char b[] = "rwptolhr.txt";
}
else if (fchoice == 2)
{
char b[] = "lhrtokch.txt";
}
else if (fchoice == 3)
{
char b[] = "rwptokch.txt";
}
fstream file;
file.open(b, ios::in | ios::app);
if (!file)
{
cout << "\n\nError in opening file!!!" << endl;
}
cout << "\n\nFile content: " << endl;
//reading and extracting data from file.
for (string line; getline(file, line);)
{
while (string::npos)
{
int count = 0; //to count the number of name characters before space
for (int i = 0; line[i] != ' '; i++)
{
count++;
}
string customername = line.substr(0, count);
cout << "\n\nName: " << customername << endl;
int fcount = 0; //to count the number of age characters before space
for (int i = count + 1; line[i] != ' '; i++)
{
fcount++;
} //please note that "+1 +2 etc in the counter //variables" are given because there is spaces in the string.
string customerfrom = line.substr(count + 1, fcount);
cout << "From: " << customerfrom << endl;
int tcount = 0; //to count the program characters
for (int i = count + fcount + 2; line[i] != ' '; i++)
{
tcount++;
}
string customerto = line.substr(fcount + count + 2, tcount);
cout << "To: " << customerto << endl;
int pcount = 0; //to count the number of name characters before space
for (int i = count + fcount + tcount + 3; line[i] != ' '; i++)
{
pcount++;
}
string customerpayment = line.substr(count + fcount + tcount + 3, pcount);
cout << "Payment: " << customerpayment << endl;
int ocount = 0; //to count the number of name characters before space
for (int i = count + fcount + tcount + pcount + 4; line[i] != '\0'; i++)
{
ocount++;
}
string customeroid = line.substr(count + fcount + tcount + pcount + 4, ocount);
cout << "Order id: " << customeroid << endl;
break;
}
}
}
Changing the char b[13] to string b has worked for me.
else if (choice == 4)
{
string b;
cout << "\n\nPress 1 to display Rawalpindi to Lahore Queue";
cout << "\nPress 2 to display Lahore to Karachi Queue";
cout << "\nPress 3 to display Rawalpindi to Karachi Queue" << endl;
cin >> fchoice;
if (fchoice == 1)
{
b = "rwptolhr.txt";
}
else if (fchoice == 2)
{
b = "lhrtokch.txt";
}
else if (fchoice == 3)
{
b = "rwptokch.txt";
}
fstream file;
file.open(b, ios::in | ios::app);
if (!file)
{
cout << "\n\nError in opening file!!!" << endl;
}
cout << "\n\nFile content: " << endl;
//reading and extracting data from file.
for (string line; getline(file, line);)
{
while (string::npos)
{
int count = 0; //to count the number of name characters before space
for (int i = 0; line[i] != ' '; i++)
{
count++;
}
string customername = line.substr(0, count);
cout << "\n\nName: " << customername << endl;
int fcount = 0; //to count the number of age characters before space
for (int i = count + 1; line[i] != ' '; i++)
{
fcount++;
} //please note that "+1 +2 etc in the counter //variables" are given because there is spaces in the string.
string customerfrom = line.substr(count + 1, fcount);
cout << "From: " << customerfrom << endl;
int tcount = 0; //to count the program characters
for (int i = count + fcount + 2; line[i] != ' '; i++)
{
tcount++;
}
string customerto = line.substr(fcount + count + 2, tcount);
cout << "To: " << customerto << endl;
int pcount = 0; //to count the number of name characters before space
for (int i = count + fcount + tcount + 3; line[i] != ' '; i++)
{
pcount++;
}
string customerpayment = line.substr(count + fcount + tcount + 3, pcount);
cout << "Payment: " << customerpayment << endl;
int ocount = 0; //to count the number of name characters before space
for (int i = count + fcount + tcount + pcount + 4; line[i] != '\0'; i++)
{
ocount++;
}
string customeroid = line.substr(count + fcount + tcount + pcount + 4, ocount);
cout << "Order id: " << customeroid << endl;
break;
}
}
}
Output showing me only the first letter of string
//program to reverse all words except corner words
#include <bits/stdc++.h>
using namespace std;
void printReverse(string str)
{
//print first word
int i = 0;
for (i = 0; i < str.length() && str[i] != ' '; i++)
cout << str[i];
//print middle word
string word = "";
for (i = 0; i < str.length(); i++)
{
if (str[i] != ' ')
word += str[i];
else
{
reverse(word.begin(), word.end());
cout << word << " ";
word = "";
}
}
//print last word
cout << word << " ";
}
int main()
{
string str;
cout << "Enter the string: ";
cin >> str;
printReverse(str);
return 0;
}
Program to reverse all words except corner words. I'm not able to recognize what's wrong. It showed me only the first word of the string and the rest part of the code is working. Please help me.
public class HelloWorld {
public static void main(String[] args) {
String str = "Reverse string except corner";
int left = 0, right = 0;
int i = 0;
for(i = 0; i < str.length() && str.charAt(i) != ' '; i++)
{
System.out.print(str.charAt(i)); // Printing the first word
}
left = i;
String last = " ";
for(int j = str.length()-1; j >=0 && str.charAt(j) != ' '; j--)
{
last = str.charAt(j) + last; // Storing the last word
right = j;
}
String mid = str.substring(left, right); // Getting the middle words
for(int k = mid.length()-1; k >= 0; k--)
{
System.out.print(mid.charAt(k));
}
System.out.println(last);
}
}
Output
int line = 0;
string teststring = " ";
string stringarray[100];
while (codeFile.good())
{
getline(codeFile, teststring, ' ');
if(teststring!="" && teststring[0]!='\n' && teststring[0] != 9 && teststring[0] != 10 && teststring[0] != 32 && teststring[0]!=' '
&& teststring!=" " && teststring!=" ")
{
stringarray[line]=teststring; // still stores whitespace :(
cout << stringarray[line] << endl;
line++;
}
}
Hello, I am going through a text file and trying to store each string inside an element of an array but, am having problems with elements storing completely white space.
I have just solve a similar problem, how about this code:
while (codeFile.good())
{
getline(codeFile, teststring);
for(size_t idx = 0; idx < teststring.size(); i++) {
size_t start = idx;
while (teststring[i] != ' ') {
idx++;
}
stringarray[line] = teststring.substr(start, idx - start);
cout << stringarray[line] << endl;
line++;
}
}
Ignoring all the white spaces is exactly what operator>> does.
Your snippet can be rewritten as:
// ...
std::string word;
std::vector<std::string> words;
while ( codeFile >> word )
{
if ( word.empty() ) continue;
std::cout << word << '\n';
words.push_back(std::move(word));
}
for(listOfStringsIterator = listOfStrings.begin(); listOfStringsIterator != listOfStrings.end(); listOfStringsIterator++) {
string finalstring = listOfStrings.front();
string kmerStrings;
for(int a = 0; a < finalstring.length(); a++) {
if(finalstring[a+2] == NULL){
string nextLine = listofStrings.at(listofStrings.front() + 1);
kmerStrings = string() + finalstring[a] + finalstring[]
std::cout << nextLine << std::endl;
}
else{
kmerStrings = string() + finalstring[a] + finalstring[a+1] + finalstring[a+2];
std::cout << kmerStrings << std::endl;
}
I'm trying to move on to the string after the one i'm currently on after I don't have enough chars to create a 3 letter sequence.
Naturally I'm a beginner. I'm trying to create a simple phrase guess game. So I have randomly generated a phrase. I want to set up a variable equal to the length of the random phrase, which will change every game. As the game progresses and correct guesses are made I want to fill in the under scores with correct guesses.
t _ e (space here) p _ r a _ e
this sort of thing. Right now I'm just trying to generate the reveal phrase with spaces and underscores. Thanks in advance.
string phrase = verbs[random_word1] + ' ' + nouns[random_word2] + ' ' + conjunct[random_conjunct] + ' ' + result + ' ' + adjectives[random_word3];
string revealPhrase = "";
for (i=0; i < phrase.length(); i++){
if (phrase.at[i] == ' ')
revealPhrase = ' ';
else
revealPhrase = revealPhrase + "_ ";}
cout << revealPhrase << endl;
Perhaps
for (int i=0; i < phrase.length(); i++){
if (phrase[i] == '-')
revealPhrase += " "; // assuming you want spaces here
else
revealPhrase += "_ ";
}
try something like this
char c;
string actualPhrase = "the phrase";
string revealPhrase = "__________";
int length = actualPhrase.length();
int curIndex;
while(actualPhrase.compare(revealPhrase) != 0)
{
cin >> c;
for(int i=0; i < length; i += 1)
{
if(actualPhrase[i] == c)
{
revealPhrase[i] = c;
}
}
}