Please help, the print (line) will return Hello$ Python#. It should be "Hello Python"
line = "Hello$ Python#"
for char in line:
if char in "$#":
line.replace(char,'')
print (line)
The problem with what your doing is: You are altering the string line in a loop using the replace method without assigning it to anything. You should create a new variable to store the new string.
Try this:
line = "Hello$ Python#"
return_line = ""
for char in line:
if char not in "$#":
return_line += char
print(return_line)
Related
I am trying to split string ,but unfortunately strtok behaves weirdly
I have following string get|user=password|23|info|hello I have tried widely used method using strtok, but unfortunately it treats = as delimiter and I cannot parse my string.
So get parsed correctly than parsed only user, but not user=password.
Please help to find the problem or suggest any other way to split the string.
I am programming for Arduino.
Thanks
Code
const char delimeter = '|';
char *token;
token = strtok(requestString, &delimeter);
// Handle parsed
token = strtok(NULL, &delimeter);
From cppreference,
delim - pointer to the null-terminated byte string identifying delimiters
The requirement that your approach doesn't fit is null terminated. You take the address of a single char, but clearly you cannot access anything past this one symbol. strtok, however, searches for \0 character which terminates the string. Thus you're entering undefined behaviour land.
Instead, use
const char* delimiter = "|";
Change this:
const char delimeter = '|';
to this:
const char * delimeter = "|"; // note the double quotes
I am having an issue with printing a string which im using for debug purposes.
I create the string like so:
//checker is int
std::stringstream buttonx;
buttonx << "Button" << checker << "_x";
Now i try to print it to my error.txt file
FILE * p;
p = fopen ("error.txt","w");
fprintf(p, "%s" , buttonx.str());
fclose(p);
The output is:
,æ0
Its different every time. I'm not sure whats going on was hopeing some could explain the mistake?
fopen is plain C and cannot handle std::string. You need to input a char*, which you can access by calling .c_str() on the string, like this:
fprintf(p, "%s", buttonx.str().c_str());
the function fprintf wants a null terminated string (a C string); you need the c_str() instead of yours:
buttonx.c_str()
I am having trouble reading a newline character with a stringstream object.
I know when type this:
stringstream stringObj;
stringObj << "Type Line 1 \rType Line 2";
CCLabel label = ...;
label->setString(stringObj->str()->c_str());
Output from string Obj
Type Line 1
Type Line 2
But when I read in from a char*, the \r is read in literally.
char* charString = GetMyAppString(stringkey);//returns char* to string "Type Line 1\rType Line 2"
stringstream stringObj;
stringObj << charString;
CCLabel label = ...;
label->setString(stringObj->str()->c_str());
I get the literal of the string.
Type Line 1\rType Line 2
What can I do to read this string and get it formatted like in my first output example?
My goal is to take my character sequence from a char* and have the formatting apply (with \n or \r). It doesnt have to be a stringstream, but nothing else seems to work...
I'm not quite sure even after reading the documentation how to do this with sscanf.
Here is what I want to do:
given a string of text:
Read up to the first 64 chars or until space is reached
Then there will be a space, an = and then another space.
Following that I want to extract another string either until the end of the string or if 8192 chars are reached. I would also like it to change any occurrences in the second string of "\n" to the actual newline character.
I have: "%64s = %8192s" but I do not think this is correct.
Thanks
Ex:
element.name = hello\nworld
Would have string 1 with element.name and string2 as
hello
world
I do recommend std::regex for this, but apart from that, you should be fine with a little error checking:
#include <cstdio>
int main(int argc, const char *argv[])
{
char s1[65];
char s2[8193];
if (2!=std::scanf("%64s = %8192s", s1, s2))
puts("oops");
else
std::printf("s1 = '%s', s2 = '%s'\n", s1, s2);
return 0;
}
Your format string looks right to me; however, sscanf will not change occurences of "\n" to anything else. To do that you would then need to write a loop that uses strtok or even just a simple for loop evaluating each character in the string and swapping it for whatever character you prefer. You will also need to evaluate the sscanf return value to determine if the 2 strings were indeed scanned correctly. sscanf returns the number of field successfully scanned according to your format string.
#sehe shows the correct usage of sscanf including the check for the proper return value.
I've used the following code for getting a string and using its first character to make another string:
char gramG[100],aug[100],start;
cout<<"\nEnter the grammar:\n";
cin.getline(gramG,100,'.');
start=gramG[0];
aug[0]=start;
aug[1]='\'';
aug[2]='-';
aug[3]='>';
aug[4]=start;
aug[5]=char(13);
cout<<aug;
cout<<aug[0];
In the above code when i'm printing 'aug' it prints as ' ¶'->A ' if A is my start symbol. If i am printing only aug[0] then it is printing correctly A. But when i am printing the string as a whole the aug[0] value is printed as some garbage. Please help.
aug is treated as a 0-terminated character array. 0-terminate it.
aug[6] = 0;
you have to terminate the string with null value
for c++ the null value is \0
use this
aug[6] = '\0';
this should work;