Substring console output using std::string_view [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to substring console output of an std::string using std::string_view?
For example:
std::string toolong {"this is a string too long for me"};
std::string_view(toolong);
// do something...
expected console output: this is a string

Yes, it's called substring-ing.
std::string toolong {"this is a string too long for me"};
std::string_view view(toolong);
std::cout << view.substr(0, 16);
Alternatively, you can use the remove_prefix() and remove_suffix() methods as well.
Example:
view.remove_suffix(16); // view is now "this is a string"
view.remove_prefix(5); // view is now -> "is a string"
If you want to do it in-place without creating a variable of string_view, use substr()
std::string toolong {"this is a string too long for me"};
std::cout << std::string_view (toolong).substr(0, 16);

Related

Why can't I compare s, or set values equal to string s? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
vector<string> dictionary = {"dog", "cat", "yard"}
string current_letter = "y"
// traverses dictionary and locates strings that start with corresponding letter
for (const string& s : dictionary)
{
if (s[0] == current_letter)
{
// prints out word that matches current_letter
}
}
Hello, I am trying to find string in a vector dictionary that start with the 'current' letter but I am running into C++ no operator matches these operands operand types are: const char == std::string" in the if condition.
How do I fix this?
You cannot compare a char (s[0]) with a string (current_letter) which is what the error is telling you. You can make current_letter a char or use std::string::starts_with (c++20)

How to randomly display an user input string using c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to randomly display a user input string using c++ but I couldn't find a way to do.
Currently I am pre defining some strings
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
srand(time(0));
const string wordlist[4] = {"hi","hello","what's up","wassup"};
string word = wordlist [rand()%4];
cout<<word;
return 0;
}
What I want is:- I don't want to pre define them. I want the user to type in 4 words and I will display a word from the 4 words given by the user (randomly).
To do that you will have to first remove const qualifier from wordlist array.
srand(time(0));
std::vector<string> wordlist(4);
for(auto& s: wordlist) std::cin>>s;
string word = wordlist [rand()%4];
cout<<word;
Line 3 is C++11's range based for loop, this way I can easily loop over elements of std::vector<string> without indices.
If there are multiple words in a string then use getline(cin,s) accordingly. Then input each string in a new line. But be careful when mixing cin and getline for taking input.
You can use std::array if the size is going to be fixed (i.e. 4) as mentioned in comments.

Convert text (words) to integer [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
So I was making guessing game to learn something (I'm a beginner) and at the start I faced the problem that I didn't know how to do rng or something like that so I came up with idea where I just ask user to give his name and something he hates and I need to make this input somehow change to number (any kind except binary).
So in short I need a way to change any inputted text (string) to any integer number.
Here is an example of how to use a hash function, that already exists for the standard library.
#include <cstdio>
#include <string>
#include <iostream>
#include <functional>
int main() {
std::cout << "Starting Process!" << std::endl;
std::cout << "Enter Name: ";
std::string name;
std::cin >> name;
std::hash<std::string> hash_fn;
size_t str_hash = hash_fn(name);
std::cout << str_hash << '\n';
return 0;
}
I would suggest you to take a look at the "random" library if you want rng or to use hashing as other suggested.
If you want to generate yourself pseudo-random numbers from an input string, you could do the following:
To acces a character, you could use the [ ] operator, something like that:
std::string str = "The Name";
str[index]; // to acces a character
Cast the characters into integers to work with them. Keep in mind that every character has an ASCII value, so you could do something like this:
static_cast<int>(str[2]);
Now you have accesed the third character, according to the string above, 'e'. Which happens to be 101 when transformed into an int (remember, the ASCII value).
You can then create some algorithm using that.

c++ string += operator memory consequence [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am wondering how std::string handles its buffers in terms of memory.
As I understand it char buf[BUFFER_SIZE]; will be allocated on the stack. So if it begin reading into that buffer
string result;
oFile.read(buf, BUFFER_SIZE-1);
bytesRead = oFile.gcount(); //get # of chars read into buffer
buf[bytesRead] = '\0'; //terminate with a null
sFinal += buf;
So my question is mainly on the += operation. When the buffer is concatenated with the string, does it need to reallocate more memory? As a follow up question does this memory need to be a continuous block? If so, would that allocation be a heap or stack operation?
std::string is an object in C++, then implicit constructors get char pointers as string to support C's string literals.
If you care about memory a std::ostringstream can be more useful that the + operator:
std::ostringstream result("hello");
result << " world!";
std::cout << result.str();
//prints "hello world!"

Cstring input using get() c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Trying to write the following function but confused, as get() only reads in the first character?
Write C-string's chars to the screen one char at a time.
void writeString(const char*)
Rule:
cannot use [].
Hints:use put();
make use of '\0' – but don't write it out.
It sounds like you just need a simple loop to output the string. Something like this perhaps.
void writeString(const char* str)
{
while(str++ != '\0') put(*str);
}
The while(str++ != '\0') will iterate over the string buffer pointed to by str and output each character. It also increments the str pointer to the next character and checks for null terminator ('\0').