Is it possible to modify string input? [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 5 years ago.
Improve this question
I want to build a program to save text to files , but I want my program to secure or encrypt the content of the text , for example , if the user input "salamence" to the program , the program would output (into a file) "hjkjupfqp" or something like that so people can't read it unless they have access to the program (I want the program to be able to decrypt the text file too) so it is possible in c++ to read strings input one by one character and modify them into another characters , and how to do that ?

A string is a sequence of chars put in a container that has other stuff in it. The chars themselves can be accessed through the [] operator. A char is basically an 8-bit integer that can be displayed. An integer can be manipulated arithmetically(+,-,*,...), bit-wise(&,^,|,<<,...), etc.
So you could do something like this:
#include <iostream>
#include <string>
using namespace std; //bad idea, but simplifies stuff
int main(){
string s;
cin>>s; //reads the string
for(int i=0;i < s.size;i++){ //loops through all characters of the string
s[i]++; //adds one to the string
}
cout<<s; //outputs the modified string
}
This will turn "abc" into "bcd", wich is a rather stupid form of encryption, but it proves the concept.
To decrypt you would need to copy the loop, but replace s[i]++ with s[i]--.
Since you seem to be a beginner, I would actually recommend using c-style strings, but that is outside the scope of this question.

Related

How can I store 'space' character into a list [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 2 years ago.
Improve this question
I have declared this vector
vector<char> germat{ SekuencaEfjaleve.begin(), SekuencaEfjaleve.end() };
where SekuencaEfjaleve is a string that i get as an input from the user. The input always contains a space in the middle, so the user inputs something like this 423 fgfh=, and when I print out the list it stops at 3 it has only 3 elements.
I read it as cin >> SekuencaEfjaleve; and i print it as
cout << germat[i];
}```
Why not you use string rather then using character type vector, like this way, it is lot easier to use.
string germat;
getline(cin, germat); // used C++ builtin function `getline()` for taking string input with spaces
// now you can access by `germat[index]`
or you can take your SekuencaEfjaleve string with getline(), like:
getline(cin, SekuencaEfjaleve) and now you can access with SekuencaEfjaleve[index].

How to set up a std::string, so that appending to it fails (or will not be observable)? [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 2 years ago.
Improve this question
#include <iostream>
using namespace std;
int main() {
string s1 = "hello ";
string s2 = "world";
s1.append(s2);
cout<<s1;
}
I can control on s1 value but can't control s2 value or change/add any other introduction in this code.
What can I do so even the code use s1.append(s2) s2 will not append to s1. (So the output will be hello ) in Linux
I tried to s1="hello \0 bbb"; but the output still was hello world.
Is there any way to fail this append?
The goal is I take s1 and create linux file with this name so I don't want the filename contain s2 string
Presumably this came from some kind of quiz.
The answer is: you can't.
With a C program you'd probably be using null-terminated buffers, and the solution would be relatively easy. But std::string is binary safe so there is nothing you can put into s1 that will "break" the string or prevent an appended s2 from being meaningful.
There may be some terminal/platform-specific tricks you can pull from up your sleeve in order to get this going at that level, when your terminal receives the data from your program via cout, but I can't think of any off-hand. Except for maybe the EOT control character (but only if you're on Windows; end-of-file is signalled in a different way on Linux and macOS), depending on what you're actually doing with the output.

get characters (not separated with spaces) in array using scanf() [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 5 years ago.
Improve this question
I need to store characters from user's input in array, BUT not one by one. User will input them as one line like this;
....
I need to save each dot in array, but I can't do this:
scanf("%s%s%s%s", &s[0], &s[1], &s[2], &s[3])
because user can enter N number of dots. So it must be dynamic, I guess.
scanf() is a C runtime function. In C++, you should be using std::cin instead. For instance, with std::getline(). You can treat the returned std::string like an array of characters.
User will input them as one line like this; .... I need to save each dot in array,
C solution:
Define upper sane bound like 1000 and use a scanset "%[]".
// Read up to 1000 `.`
char dot[1000 + 1];
if (scanf(" %1000[.]") == 1) {
// Success
puts(dot);
}
Additional code needed if other non-., non -white-space characters need to be handled.

Can someone explain these lines for purpose of buffer and c strings [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
getline(userfile, buffer);
new_user.birth_year = atoi(buffer.c_str());
for (stringstream s(buffer); s >> id;)
{
new_user.friends.push_back(atoi(id.c_str()));
getline(userfile, buffer);
Reads in a line of text into the variable buffer.
new_user.birth_year = atoi(buffer.c_str());
The atoi function requires a C-Style string. The method c_str() returns a C-Style string from the std::string.
The atoi function converts a C-Style string to an integer.
The integer result is then assigned to new_user.birth_year.
for (stringstream s(buffer); s >> id;)
{
The first part of the for loop, stringstream s(buffer), creates a string stream from the buffer string. The stringstream allows a string to be treated as a stream.
The s >> id reads a value from the string and places the result into id.
new_user.friends.push_back(atoi(id.c_str()));
This line converts the id string to an integer (see above about atoi) and appends the integer to the vector friends inside the new_user object.
This apparently expects to read some data in a format something like this:
1992 17 3 34 9
The first number is a user's year of birth. The numbers after that are ID numbers of that person's friends.
The first bit (new_user.birth_year = atoi(buffer.c_str());) reads the first number and assigns it to new_user.birth_year.
The next loop reads the rest of the numbers and adds them to a collection (probably std::vector) holding the IDs of that user's friends. Minor probable bug: it looks like it adds their birth year as an ID of a friend, along with the rest of the IDs numbers.

Odd characters in output [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to write a simple hangman game in c++ by randomly selecting a word from a list, checking the string length, and writing that many *s into a new string to serve as placeholders in the yet un-guessed word. The max length is 9 letters. I have the game working almost flawlessly -- the problem is that whenever my word has 8 or 9 letters, the program prints the correct number of *s followed by one or two � characters. Research tells me these are unprintable characters, but I've tried for a while now and I'm not sure why they're here, why they only show up with a word length>7, or how to get rid of them. Below is relevant code. Any suggestions?
Generating *s:
char word[80];
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word[i]='*';
}
You forgot to add the \0 terminator at the end of the string. After the for loop, add:
word[i] = '\0';
Or, best, use std::string instead of a C string.
Try using std::string instead.
std::string word;
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word+='*';
}