-
Stuck....
I think I tried to post it here before but it didn't work. Im not askin for the full solution, the deadline is passed anyway but this has been keeping me up nights.
"Write a function scanstring that works like scanf with a %s placeholder - it should skip leading whitespace and copy a string up to the next whitespace character. It should have 2 parameters: the string variable into which the string is copied, and the amount of space available in the string. It should use getchar to read in the string characters.
"
This is an assignment I was given, I couldnt figure out how to test for whitespaces and I don't know how to use the Cstring::trimleft...
Any help with either would be appreciated.
-
You could use std::string, and use the find_first_not_of member function to find the first position that is not a whitespace, and then use the find_last_of member function to find the last character that is not a whitespace. Then you could use the substr member function to create a substring based on the two positions obtained to create a trimmed string. You could also write some predicate function for the transform algorithm which would trim strings also, but since I don't have a compiler or any of my code in front of me, that will be up to you to figure out. Both ways work.
-
Function declaration
void Scanstring ( char a[], int size);
Code:
int ch;
while( (ch = getchar() ) = '\n' || size != 0)
{
//skip whitespace
if ( ch == ' ' ) continue;
else
{
//Than add ch characters to the array a[]
-- size;
}
}
I believe that this is the general idea although simplified and not complete.
-
I'm not sure if it was you or someone else, but I do remember a question similar to this. Do a search, although I'm not sure what you would search.