I'm having trouble with getting this code to work properly.
It's purpose is to show the middle word of any given sentence. If its an even amount of words, it shows the first word out of the 2. Instead of printing the middle word, it prints the 2 middle characters. I think its only a few small things I have to add, but I'm stuck on this roadblock. Any help would be appreciated, thank you!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string sentence="";
string letter="";
string middle="";
int count=0;
int spacecount=0;
int mid=0;
cout << "Enter a sentence:" << endl;
getline(cin,sentence); //gets user input
//for example, "hello there friend"
for(int count =0; count<sentence.length();count++){
letter=sentence.substr(count,1);
if(letter!=" "){
count++;
}
if(letter==" "){
spacecount++;
}
if(count> mid){
mid = count;
}
if (((mid = sentence.length() / 2) % 2) == 0){ //checks if amount of words is even
middle=sentence.substr(mid,2);
}
if (((mid = sentence.length() / 2) % 2) >= 1) //checks if amount of words is odd
{
middle=sentence.substr(mid,2);
}
}
reverse(middle.rbegin(),middle.rend()); //makes it so the word isnt backwards
cout <<"Middle word is: " << middle <<endl;
//shows middle word to user
//it should print "there", but it shows "he" (the two middle characters)
return 0;
}
Lets split the string into tokens and find the middle object of it?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char **argv) {
string input;
getline(cin, input);
vector<string> tokens;
string token;
for (size_t i = 0; i < input.length(); i++) {
char c = input[i];
if (c == ' ' || !input[i + 1]) {
if (!input[i + 1])
token += c;
tokens.push_back(token);
token = "";
continue;
}
token += c;
}
auto mid = tokens.size() % 2 == 0 ? tokens.begin() + tokens.size() / 2 - 1
: tokens.begin() + tokens.size() / 2;
cout << *mid;
return 0;
}
Related
Wrote a function that calculates the average length of words in a sentence.
Why does the program print 0 instead of the average?
Please help me fix my mistake.
If you know how to make an implementation in one function, please write.
#include <iostream>
#include <string>
using namespace std;
int CountWordsAndLetters(char* str, int& words, int& letters)
{
words = 0;
int i = 0;
letters = 0;
while (str[i] == ' ')
i++;
for (; str[i]; i++) {
if (((str[i] >= 'a') && (str[i] <= 'z'))
|| ((str[i] >= 'A') && (str[i] <= 'Z')))
letters++;
if (str[i] == ' ') {
words++;
while (1)
if (str[i] == ' ')
i++;
else {
i--;
break;
}
}
}
words = words + 1;
return (words);
}
float AverageLetters(float words, float letters)
{
float a = (double)(letters / words);
return a;
}
int main()
{
char array[255];
int words = 0;
int letters = 0;
cout << "Enter the string\n\n";
gets_s(array);
int size;
for (size = 0; array[size]; size++)
;
char* str = new char[size];
CountWordsAndLetters(str, words, letters);
cout << "\nAverage number of letters per word: "
<< AverageLetters(words, letters);
return 0;
}
If you know how to make an implementation in one function, please write.
Here, you are allocating an uninitialized array of char:
char* str = new char[size];
You put nothing in it.
You then pass it to CountWordsAndLetters:
// here -------v
CountWordsAndLetters(str, words, letters);
You should consider simply sending array instead:
CountWordsAndLetters(array, words, letters);
Here's a live example of your code working.
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.
I need help completing a task for class, basically I need to read letters from a text file, count each letters occurrence and sort them by most occurrences. I'm having a problem remembering each letters original count after sort (since index changes)
The problem is that the result letters are appearing more then once so I lose some of the letters
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main ()
{
char letter, b[26] = {};
int number[26] = {0}, temp, i, j;
ofstream outfile ("test.txt");
srand (time(NULL));
for(int i=0;i<20;i++){
char c = rand() % 26 + 65 + rand() % 2 * 32;
outfile <<c<<endl;
}
outfile.close();
ifstream file( "test.txt");
while(file >> letter){
if (letter >= 'a' && letter <= 'z') number[letter - 'a']++;
if (letter >= 'A' && letter <= 'Z') number[letter - 'A']++;
}
for(i=0; i<26; i++)
{
for(j=i+1; j<26; j++)
{
if(number[j] > number[i])
{
temp = number[i];
b[i] = char(97+j);
number[i] = number[j];
number[j] = temp;
}
}
}
for(i=0;i<26;i++){
if(number[i] != 0)cout<<b[i]<<" "<<number[i]<<endl;
}
return 0;
}
l 3
m 3
m 2
q 2
w 2
j 1
l 1
m 1
o 1
q 1
t 1
v 1
w 1
To broaden what i was saying in comments about using a struct, consider this:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct CharInstance {
int count;
char letter;
};
int main()
{
char letter;
int i, j;
CharInstance temp;
CharInstance charInst[26] = { 0 };
// create test data randomly
{
ofstream outfile("test.txt");
srand(time(NULL));
for (int i = 0; i < 500; i++) {
char c = rand() % 26 + 65 + rand() % 2 * 32;
outfile << c << endl;
}
outfile.close();
}
// read in data and store in array
{
ifstream file("test.txt");
while (file >> letter) {
// force lowercase to uppercase so we count both lowercase and uppercase as the same
if (letter >= 'a' && letter <= 'z') letter -= 32;
// since all input is now uppercase, ignore anything that isn't in uppercase range
if (letter < 'A' || letter > 'Z') continue; // skip non-alpha chars
const int ind = letter - 'A';
++charInst[ind].count;
charInst[ind].letter = letter;
}
file.close();
}
// bubble sort
for (i = 0; i < 26; i++)
{
for (j = i + 1; j < 26; j++)
{
if (charInst[j].count > charInst[i].count)
{
temp = charInst[i];
charInst[i] = charInst[j];
charInst[j] = temp;
}
}
}
for (i = 0; i < 26; i++) {
cout << charInst[i].letter << ": " << charInst[i].count << " " << endl;
}
return 0;
}
If you don't mind C++11.
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
int main() {
std::ifstream in("sort.cpp");
// Map to store counts of each character.
// Note: this won't work with some international characters (i.e. Chinese).
std::map<char, int> histogram;
char ch = 0;
while (in.get(ch)) {
// TODO
// Filter out characters you don't want, as this would also pick whitespaces
// and newlines.
// Increment the count for |ch|.
// This would create new map element if the count was zero.
++histogram[ch];
}
// Move map contents into an array of pairs character->count.
std::vector<std::pair<char, int>> pairs(histogram.begin(), histogram.end());
// Sort the array. The third parameter is a function used as a custom comparator.
std::sort(pairs.begin(), pairs.end(),
[](const std::pair<char, int>& left, const std::pair<char, int>& right) {
// Use the count to compare |left| and |right|.
return left.second > right.second;
});
for (const auto& pair : pairs) {
std::cout << pair.first << " " << pair.second << std::endl;
}
}
g++ -std=c++11 sort.cpp
./a.out
207
t 79
a 62
e 61
r 61
i 53
s 51
o 50
n 50
c 46
...
I have made this code such that whatever I type in a sentence has the first letter of the first word capitalized; While reducing any number of spaces in a sentence to just one space. However, my sentences are only reducing by one space. For example, if I put 3 spaces in a sentence, the output has spaces reduced by 1 to 2 spaces, but I want the output of words in a sentence to have only one space. I can't quite figure out what is wrong with my code and hence any help would be greatly appreciated. I have attached my code for reference below:
#include <stdio.h>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
int i = 0; //i for counter
string str;
//String variable
getline(cin, str); //Get string from user
int L = str.length(); //Find length of string
//Display original string
for (int i = 0; i < 100; i++)
{
str[i] = tolower(str[i]);
}
str[0] = toupper(str[0]);
bool space;
for (int j = i + 1; j < L; j++)
{
str[j] = str[j + 1];
L--;
}
cout << str << endl;
return 0;
}
Or doing it in a more modern way using iterators :
#include <iostream>
#include <cctype>
int main() {
std::cout << "This is the string trimming function.\n" <<
"Throw in a string and I will make sure all extra spaces " <<
"will be reduced to single space.\n";
std::string InputString, TrimmedString;
int head = -1;
int tail = -1;
std::cout << "Please enter the input string :\n" << std::endl;
std::getline(std::cin, InputString);
for(std::string::iterator it = InputString.begin(); it <= InputString.end(); it++){
if (*it != ' ' && head < 0 && tail < 0) {
head = std::distance(InputString.begin(), it);
}
else if (head >= 0 && tail < 0 && (*it == ' ' || it == InputString.end())) {
tail = std::distance(InputString.begin(), it);
TrimmedString.append(InputString, head, (tail-head));
TrimmedString.push_back(' ');
head = -1;
tail = -1;
}
}
TrimmedString[0] = toupper(TrimmedString[0]);
std::cout << "\nThe processed string is :\n\n" << TrimmedString << std::endl;
return 0;
}
Try this:
int main()
{
std::string str;
std::getline(cin, str);
//Change case
str[0] = toupper(str[0]);
std::transform(str.begin() + 1, str.end(), str.begin() + 1, ptr_fun<int, int>(tolower));
//Parsing time
for (int i = 0; i <= str.size() - 1; i++)
{
if (str[i] == ' ' && str[i + 1] == ' ') //if present & next are whitespaces, remove next
{
str.erase(str.begin() + i);
i--; // rechecking condition
}
}
std::cout << '\n' << str << '\n';
}
Output:
#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.