C-String, SubStr, and Strtok | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C-String, SubStr, and Strtok

So, this might be the most confusing part of C++ for me thus far. Even having my friend show me a code for supposedly SubStr and Strtok has me dumbfounded (See his example code below) from what i understand it removes the spaces and any/all punctuation. But, what am I supposed to call if i wanted to compare it to another string? Admittedly this is a semi followup question to my question from (https://www.sololearn.com/Discuss/1538466/confused-on-code-error-c) due to an error on my part. tokenptr = strtok(myArray, " .?!"); while (tokenptr != "\0"){ cout << tokenptr; tokenptr = strtok("\0", " .?!");

16th Oct 2018, 10:18 PM
Ryan Finley
5 Answers
+ 3
Hey Ryan Finley, sorry about the delay, well, I think I just figured out how it's done and made a code for it, honestly this way of Palindrome checking is kinda new to me, as I'm more familiar with the common method, by comparing the characters from each of the opposite end of the string and don't usually bother removing whitespace or punctuation. But thanks to this, I learned a few C-string functions along the way : ) I put some comments on the code to explain some stuffs, I hope they are readable fine (English wasn't my native language). If not, feel free to confirm either through code comments, or on this thread. https://code.sololearn.com/cs825QP42Q6W/?ref=app
18th Oct 2018, 11:08 PM
Ipang
+ 2
That's my fault, (frustration last night threw me off a bit) essentially i'm trying to make a code that takes input from a user, and from what i can understand put it into a strtok loop eventually the output will simply be removing the punctuation and spaces, then outputting it backwards. I understood how to do the majority of that until i realized i had to use Strtok, and Substr. If anything i could just use more help figuring out Strtok and Substring more than anything. Cause that's whats holding me back at this time.
17th Oct 2018, 9:25 PM
Ryan Finley
+ 2
Hey Ryan Finley, don't get frustrated over this, remember you have friends here in SoloLearn. Alright, I think I might be able to help a bit, but I just want to confirm some things first, so I could be sure I'm doing things as expected. 1. Is the use of strtok strictly necessary, or is required (lecturer's instructions for example), if not, we can try and find other ways to get rid of the spaces and punctuations to build substring, the string reversal process can be done after that. 2. From the original post above, you asked about how to compare it to another string, did you mean compare user input string with other string, or the processed string (with no punctuations, reversed) with other string? and why the need for comparison? Example: ● Input "Hello World! how are you today?" ● Result: "yadotuoyerawohdlroWolleH" Is that what you after? the result is a reversed input with all punctuation and spaces had been removed. Please correct me if I get you wrong : )
18th Oct 2018, 7:18 AM
Ipang
+ 2
Thank you for the vote of confidence, first off. But to explain a bit more, Unfortunately I have to use Strtok, i went through the code doing everything i thought was required until I realized the use of Strtok was required. That would be where i got lost. In regards to the actual instruction. i was/am supposed to get a c-string input from a user and check if it's a palindrome or not (which is where my earlier question/post came from) it would be sent to a function to remove space/punctuation, and make everything lowercase, BUT I had to user StrTok to do that (which is what threw me off). all i had to do afterwards was simply check if the end result of all the editing made the word a Palindrome, then to simply output it if so. Honestly, as I've said, it's quite literally the usage of StrTok that has stopped me in my tracks. Otherwise, aside from now making everything lowercase you have the example output input correct (assuming the input is a palindrome of course).
18th Oct 2018, 7:58 AM
Ryan Finley
+ 1
Can you please explain what you are trying to do with that snippet? I still don't understand what your intention was, even after having read that previous discussion thread. Provide some examples of input, and what output you want may help to understand better.
17th Oct 2018, 10:44 AM
Ipang