Ignoring punctuation when copying cstrings

First time post so forgive me if there is some rules I don't follow.

Now, I have an assignment in which I am to accept arguments from the command line and copy them into a cstring and display said cstring unmolested. Then I should store it into another cstring but ignore all punctuation, spaces and capital letters. Like this:

./a5 Marge lets Norah see Sharon\'s telegram
As is: Marge lets Norah see Sharon's telegram
---->: margeletsnorahseesharonstelegram
<----: margeletsnorahseesharonstelegram
Sequence entered is a palindrome
=======================================================

Finally I am to chaeck to see if it is a palindrome. Most of it works and if no spaces are entered nor punctuation it works. However, punctuation causes it to malfunction. Any help or hints would be greatly appreciated.

I'm just going to copy the function and if anyone wants the whole program I can post it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char *FormSeqProc (int argc, char *argv[], char seqAsIs[])
{
   int len = 0,
       n = 0;

   for (int p = 1; p < argc; ++p)
   {   
      len += strlen(argv[p]);
   }   

   char *seqProc = new char [len + argc + 1]; 
   
   for (int p = 0; p < strlen(seqAsIs); ++p)
   {   
      if (isalnum(seqAsIs[p]))    
         seqProc[n++] = tolower(seqAsIs[p]);
   }   
   
   seqProc[len + argc + 1] = '\0';

   return seqProc;
}


Thank you in advance
Last edited on
Sorry I wrote it with vim so the alignment is off.
You have to use code tags, select the program text and click the <> button.

Thank you
What're the inputs and outputs for your posted code?

I don't much sure which work this function does.

And there'll be better if you post an example in case of the problem you're facing.

Maybe adding a little comment in code must be good.
Last edited on
You are trying to do too many things at once.Try to make functions that do only one thing well.Each function should move you one step closer to the final calculation.Suggestions for functions:

1
2
3
4
5
6
7
8
9
10
11
char* args_to_string( int argc, char* argv[] );
// Converts command-line arguments to a single, newly-allocated string.
// Don't forget to delete[] the result when you are done with it!

char* copy_only_alphanums( char* s );
// Creates a newly-allocated copy of a string, where only the alnum() chars
// are copied from the original string.
// Don't forget to delete[] the result when you are done with it!

bool is_palindrome( char* s );
// Returns true only if the string is exactly the same forward and backward. 

Hope this helps.
Duoas, I know but this is for an assignment at school and we are required to do it this way. Thanks everyone I actually figured it out.
Topic archived. No new replies allowed.