Print a string store into the stack - Tricky challenge [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I found following C++ source code:
void victory()
{
printf("xxxxxxxxxxxxxxxxxxxxxxxx"); //random string
}
int main()
{
if(0) victory();
/* code here! */
}
Your main objective is to print the random string(from function victory) on stdout
How can It possible? How can I print the random string? It's a tricky traps, just for curiosity. Above all, I can't use pointers...
Rules:
Max 12 chars.
You can't use: "main", "victory", "asm", "%", "*", "_", "#", "/", "&".
You have only one semicolon.

It is pretty clear that there is no way to solve this in any remotely standards-compliant way.
Since this is being framed as a "security" question, this suggest that it's OK to use compiler- and OS-specific hacks. With this in mind, the following works on my system using gcc:
#include <stdio.h>
void victory()
{
printf("SxxxxxxxxxxxxxxxxxxxxxxE"); //random string
}
int main()
{
if(0) victory();
puts(""-25);
}
It relies on the fact that the "" gets placed by the compiler immediately after the string literal that we're trying to recover. Here, 25 is the (known) length of the unknown string.
Of course, this has UNDEFINED BEHAVIOUR written all over it in huge neon letters. Handle with care.

Related

String Encode in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to convert the string "Test €" to "Test &#8364 ;" and vice versa. Please make a note that its a string and not from the xml. For your information I am developing the application in C++ and using Xerces also for XML Parsing. Please help me how it can be achieved in c++ application.
Thanks,
Ram
I think this answer may be platform-dependent, though I don't know for sure.
You can use stringstreams and casting. If lookup is a string holding the decimal version of the character code, this function will return the character version:
char fixchar(string lookup){
stringstream converter (lookup);
int i;
converter >> dec >> i;
return (char)i
(Note that for hex strings, which are prefixed with #x instead of #, you can just use hex instead of dec).
You can get the lookup strings by using the find function on the original string. Here's a loop that uses the above function to convert a string (called fixd) with &#x[number] substrings into a normal string with no character codes:
while (fixd.find("&#x")!=string::npos){
tag = int(fixd.find("&#"));
endtag = int(fixd.find(";"));
fixd = fixd.substr(0,tag) + fixchar(fixd.substr(tag+3,endtag-tag-3)) + fixd.substr(endtag+1, fixd.length()-tag-4);
}
Similarly, you should be able to get the int version of a character just by casting it, after which you can do whatever you want with it, including adding it in decimal form to a string.

Does the cin function add null terminated at the end of input? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
If I declare a character array: char arr[200]
and then I subsequently use the function cin to read values into arr[200]
and I type into the command window line: abcd
Is there a null terminated: \0 automatically added to the array at the end of the input?
(I don't think so because I tested it using the cin function: cin>>abcd )
Can somebody explain it to me why?
Below is a snippet of my code I use to test
char arr[200]
int count=0;
int i=0;
cin>>arr // i type into command window:abcd
while (arr[i] != '\0')
{
count++;
i++
}
My count value will not be 4 but like 43 hence I concluded that the character array is not null terminated after the cin function
Formatted input from a std::istream into a character array will null-terminate the input, as specified in C++11 27.7.2.2.3/9:
operator>> then stores a null byte (charT()) in the next position
The code you've posted gives the expected result once the obvious syntax errors are fixed. But beware that this is very dangerous; there is no check on the length of the array, so too much input will overflow it. I strongly recommend you use the std::string class, rather than plain character arrays, for managing strings.
The code you posted in a comment via a link looks like this:
char array[20];
int length=getlength(array);
cin>>array;
reading into the array after attempting to measure the string length of the uninitialised array. This could give any result, or crash, or cause any other example of undefined behaviour.
In future, you should make sure that the code you post in your question is the same code that exhibits the behaviour you're asking about; otherwise, it's impossible to answer the question.
Yes, the input will be zero-terminated. Otherwise you wouldn't be able to, for example, print it without printing random characters after your input.

how to remove Ì from data in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am getting a value like this
"RE000022000500200Ì 0.00 0.1 0.129.8#####- 97- 2#####- 1##### 960.504.00 0: 00.000.00 8: 013:52 0: 021:52############2.00.0 "
I want a value like this after doing some processing on above data can you You please what should be the logic to remove "Ì"
"RE000022000500200 0.00 0.1 0.129.8#####- 97- 2#####- 1##### 960.504.00 0: 00.000.00 8: 013:52 0: 021:52############2.00.0 "
If there's no character that's value is 0xC3 in your data, you can traverse your data string, if an character that it's value equals 0xC3 occur, then remove the character and the next character.
It's easy enough if you are using std::string to hold your value.
#include <string>
#include <algorithm>
std::string input = ...;
input.erase(std::remove(input.begin(), input.end(), 'Ì'), input.end());
It's more complex if you insist on using C strings or arrays.
I see from the comments above that you are using C strings. I suggest you switch to using C++ strings.

How to tell a C++ regex to be treated as plain text / escape all characters [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am wrting a c++ function to implement string replacement. The function is like:
using namespace std;
string Replace(const string & str, const string & strOld,
const string & strNew, bool useRegex, bool caseSensitive)
{
regex::flag_type flag=regex::basic;
if(!caseSensitive)
flag |= regex::icase;
if(!useRegex)
// WHAT TO DO ?
std::regex rx(strOld,flag);
string output=regex_replace(str,rx,strNew);
return output;
}
It replaces all occurrences of strOld in str to strNew. I attempted to use std::regex and std::regex_replace to implement it. It works well in case useRegex is true. However, in case useRegex is false, I am not able to tell them that strOld is just a plain string instead of a regex string.
For example, When I call:
string output=Replace("hello.",".","?",false,true);
It returns "??????" while I expect it to be "hello?".
Halfway-solution is to pre-process the regex and escape all metacharacters manually. It's the best solution if this feature is missing from C++11 which from comments sounds like it is.

Find function doesn't return the right value [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Is it not possible to fill a map like this:
void Spel::Fill( void )
{
int buildslist[] = {3,3,2,2,2,2,3,2,2,2,2,3,3,2,2,2,2,1,1,1,1,1};
Building::buildings p;
for( int i = Building::INDIGOK; i < Building::STADSHUIS; i++)
{
p = (Building::buildings) i;
gebouwenMap[p] = buildslist[i];
}
}
This gives all 0. Building::buildings is an enum with some building names. The buildslist is a list of how many people could join that building.
First, there is not enough code to give an actual answer. Please improve your question so that we can help you properly.
Second, the title is misleading "Find function doesn't return the right value". There is no "find" function and there is no "return value" because the only function you show returns void.
Now thet this is sorted out, let me try to help you:
p = (Building::buildings) i;
This is a cast from int to enum. I think this is bad C++ (probably undefined as in might work for some compiler but not as a rule of thumb). You would have to use a switch here I think.
Please write code in English. Do you imagine if someone you work with is Japanese and would write the code with japanese variable names? Even if the project is in Dutch, write code in English including comments.
EDIT: You might want to use strings instead of an enum here.
Try using a std::map<std::string, int> to encode your building names instead of an enum, then use an std::map::iterator to iterate through it.