How to get strings until find space - c++

Hello how to get all strings until find space and push_back the words until space in the second turn of For loop to start getting all string after space and again until find space that's my code
for example this sentece 5bbbb3 1f a0aaa f1fg3
i want to get bbbb and push_back into in a vector of chars then to push_back aaaa and so
vector of chars vec = vec.[0] == 'bbbb' vec.[1] == 'aaaa' vec.[2] == 'f' vec.[3] == 'ffg'
Thank you in advanced
these are my 2 codes both does not work
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string sentece;
getline(cin, sentece);
vector<char> words;
for (int i = 0; i < sentece.size(); ++i)
{
while (sentece.substr(i, ' '))
{
if(isalpha(sentece.at(i)))
{
words.push_back(sentece.at(i));
}
}
}
cout << words[0] << '\n';
cout << words[1] << '\n';
cout << words[2] << '\n';
for(const auto& a : words)
{
cout << a;
}
return 0;
}
//==================================================================
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string sentece;
getline(cin, sentece);
vector<char> words;
for (int i = 0; i < sentece.size(); ++i)
{
while (sentece.at(i) != ' ')
{
if(isalpha(sentece.at(i)))
{
words.push_back(sentece.at(i));
}
if(sentece.at(i) == ' ')
{
break;
}
}
}
cout << words[0] << '\n';
cout << words[1] << '\n';
cout << words[2] << '\n';
for(const auto& a : words)
{
cout << a;
}
return 0;
}

I believe this code should give you the answer you want:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string test = "5bbbb3 1f a0aaa f1fg3";
vector<string> words;
for (int i = 0; i < test.size(); i++)
{
string tmp = "";
while (test[i] != ' ' && i < test.size())
{
if (isalpha(test.at(i))){
tmp += test[i];
}
else if (test.at(i) == ' ')
{
i++;
break;
}
i++;
}
words.push_back(tmp);
}
cout << words[0] << '\n';
cout << words[1] << '\n';
cout << words[2] << '\n';
cout << words[3] << '\n';
}
All you have to do is then replace the test sentence with your user input. You were forgetting to increment i in the while loop so it was examining the same character every time and getting stuck infinitely.
I tried to use as much of your original code as possible so don't assume that this is the most efficient or elegant solution to the problem
Hope this helps :)

You can use character array with scanf()
`
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[1000];
scanf("%[^' ']%s", s);
cout<<s;
}
This will stop taking input untill you hit enter but this will store string upto only till first occurence of space.

Related

Problem with letter pyramid, for loop not ending where i want it to c++

the program will make the pyramid, and then continue one line after where it starts to repeat middle of the pyramid character, and then it will crash after. Where am I going wrong in my code?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string word;
string pyramid;
string pyramid2;
int main() {
cout << "Enter word for the letter pyramid" << endl;
getline(cin, word);
string whites(word.length(), ' ');
for (int i = 0; i < word.size(); i++){
if(word.at(i) != string::npos){
pyramid.push_back(word.at(i));
if (i != 0){
pyramid2 = word.at(i-1) + pyramid2;
}
cout << whites << pyramid << pyramid2 << endl;
whites.pop_back();
}
}
for (int x = word.size()-1; x > 0; x--){
pyramid.push_back(word.at(x-1));
pyramid2 = word.at(x-1) + pyramid2;
cout << whites << pyramid << pyramid2 << endl;
whites.pop_back();
}
return 0;
}

How to read more than one line of input?

So I have built a small basic data encrypter (for learning purposes only). It is working perfectly fine but it reads only a single line of input. Is it my Editor problem or my code have some issues.
ps: I use CodeBlocks
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
std::string str;
char enc;
int word;
cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
cout << "\t\t\t\t\t\t\t\t---------" <<endl;
cout << "Enter a Word: ";
getline(cin, str);
int n = 0;
cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D#T#" <<endl;
cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
for(int i = 0; i < str.length(); i++){
int randomAdd[5] = {5,6,2,3,2};
int size = sizeof(randomAdd)/sizeof(randomAdd[0]);
// for(int j = 0; j < 5; j++){
word = str.at(i);
if(i%5 == 0){
n = 0;
}
enc = int(word) + randomAdd[n];
std::cout << char(enc);
n++;
}
return 0;
}
This works
Hello World
But I cannot enter this
Hello World
Have a nice day
because then the program exits command prompt without any error or message.
How can I read more than one line?
You can do as
#include <iostream>
using namespace std;
int main() {
string str;
while (getline(cin, str)) {
cout << str << endl;
}
return 0;
}
This code sample allows you to input multiple lines interactively from the command line/shell
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
char enc;
int word;
vector<string> myInput;
cout << "\t\t\t\t\t\t\t\tENCRYPTOR" <<endl;
cout << "\t\t\t\t\t\t\t\t---------" <<endl;
while (str != "Enigma")
{
cout << "Enter a line (Write Enigma to exit input): ";
getline(cin, str);
myInput.push_back(str);
}
int n = 0;
cout << "\n\n\t\t\t\t\t\t\t\tENCRYPTED D#T#" <<endl;
cout << "\t\t\t\t\t\t\t\t--------------\n\n" << endl;
for(auto & myInputLine : myInput)
{
str = myInputLine;
for (size_t i = 0; i < str.length(); i++) {
int randomAdd[5] = { 5,6,2,3,2 };
int size = sizeof(randomAdd) / sizeof(randomAdd[0]);
word = str.at(i);
if (i % 5 == 0) {
n = 0;
}
enc = int(word) + randomAdd[n];
std::cout << char(enc);
n++;
}
}
return 0;
}
The input is finished if Enigma is written.
All input is stored in the vector container of the STL, see vector.
Afterwards, all the lines are encrypted by your algorithm.
Hope it helps?

Prompt the user for a word then check it for double characters

I'm trying to check a word for repeated letters. For example, words like "supercalifragilisticexpialidocious," and "emphasis," should not have any double letters in it, but words like "different," "mississippi," and "formatting" should. This is what I have at the moment:
You are close, but your approach is using the wrong data type for the input, and it is outputting the result incorrectly.
Try something more like this instead:
#include <iostream> // for cout and cin
#include <string> // for string commands
using namespace std;
bool hasDoubleChars(const string &str) {
for (size_t i = 1; i < str.size(); ++i) {
if (str[i] == str[i-1]) {
return true;
}
}
return false;
}
int main() {
string str;
cout << "Welcome to the DoubleChecker(TM) word checker" << endl;
cout << "=============================================" << endl;
cout << "Enter a word to check: " << endl;
cin >> str;
if (hasDoubleChars(str)) {
cout << "There are double characters in the word " << str << ".";
} else {
cout << "There are no double characters in the word " << str << ".";
}
cout << endl;
return 0;
}
Live Demo
You can loop through each character of a string, then compare the next character in the string to the previous.
Example
#include <iostream>
int main()
{
std::string str = "Mississippi";
for (int i = str.size(); i > 0; i--)
if (str[i] == str[i-1])
std::cout << "- " << str[i] << std::endl;
return 0;
}

How to fix vector not printing values from file

I am trying to get some values line by line from a text file:
17.09 284.60 486.01 34.12 12.04 1.20 2.33 36.85 73.44
31.25 196.09 323.26 69.76 47.33 79.82 11.42 27.97 66.61
28.76 41.45 992.29 1.29 42.33 10.83 19.16 5.86 1.88
Taking these values and putting it into a vector. Each row has values to be used in a calculation.
My code:
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <vector>
using namespace std;
int main() {
ifstream xfile;
string input;
double num=0;
int count = 0;
vector <double> myvector;
cout << "Input the file: ";
cin >> input;
xfile.open(input);
if (xfile.is_open()) {
cout << "File accessed!" << endl;
while (getline(xfile, input)) {
count++;
myvector.push_back(num);
}
}
else {
cout << "File opening failed!"<<endl;
}
cout << "Numbers of lines in the file : " << count << endl;
for (int i = 0; i < myvector.size(); i++) {
cout << myvector[i] << "\t";
}
cin.fail();
return 0;
}
My output is somewhat correct, only that it is printing out just zeroes:
https://ibb.co/xqwT1hR
EDIT: the input is for the name of file. "ahu_long.txt"
You never used your num variable.
double num=0;
....
....
size_t pos = 0;
std::string token;
while (getline(xfile, input)) {
count++;
// you need convert your "input" to a double and save it to "num"
while ((pos = input.find(" ")) != std::string::npos) {
token = input.substr(0, pos);
// std::cout << token << std::endl;
num = atof(token.c_str());
myvector.push_back(num);
input.erase(0, pos + delimiter.length());
}
}
Change your variable with what you read from the file.

Display duplicate characters in a string

I wrote some code in C++ to display duplicate characters in a string, but if a character is repeated more than three times, the code prints the repeated character more than once.
For example if the string is aaaddbss, it should only print out ads but it prints aaads instead.
What am I doing wrong?
cout << " Please enter a string" << endl;
cin.getline(input, 100); // example input (ahmad wahidy) the output reads a a h a d instead of a h d
for (int i = 0;input[i]!='\0'; i++)
{
for (int j = i+1;input[j]!='\0'; j++)
{
if (input[i] == input[j])
{
cout << input[i] << " ";
}
}
}
cout << endl;
Instead of using your own custom methods, why not use a short and standard method?
Given an std::string input with the text, this will print the unique chars:
std::set<char> unique(input.begin(), input.end());
for (auto & c : unique)
{
std::cout << c << " ";
}
std::cout << std::endl;
You can use std::count and std::set:
#include <string>
#include <set>
#include <iostream>
using namespace std;
int main()
{
string s = "hellohowareyou";
set<char>the_set(s.begin(), s.end());
for (char i:the_set)
if (count(s.begin(), s.end(), i) > 1)
cout << i << endl;
}
Output:
e
h
l
o
If you are not allowed to use a map (and probably also not allowed to use a set), you could simply use an array of integers to count occurrences, with one entry for each possible char value. Note that a character - when taken as an ASCII value - can be directly used as an index for an array; however, to avoid negative indices, each character value should first be converted to an unsigned value.
#include <iostream>
#include <limits>
int main() {
const char* input = "aaaddbss";
int occurrences[UCHAR_MAX+1] = { 0 };
for (int i = 0;input[i] !='\0'; i++)
{
unsigned char c = input[i];
if (occurrences[c]==0) {
occurrences[c]++;
}
else if (occurrences[c]==1) {
occurrences[c]++;
cout << "duplicate: " << c << endl;
}
}cout << endl;
}