• C

Change String

Let's suppose I have got a string like this
char mystring[]="this is wow, hehe... okok.. that's good!";

Are there ways to remove all punctuations excluding apostrophe from the string? The resulting string should be something like this.
"this is wow hehe okok that's good"


Thanks a lot.
ee_guestAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
char *p0,*p1;
for(p0=p1=mystring; *p1=*p0++;){ if( !ispunct(*p1) || (*p1 == '\'')) ){ p1++; } }
0
 
griesshCommented:
Hi networth10,

Something like

#include <iostream>

void main()
{
   char mystring[]="this is wow, hehe... okok.. that's good!";
   printf ("%s\n", mystring);
   int i = 0;
   char c=mystring[i];
   while (c!='\0') {
       if ( (c>='0' && c<='9') || (c>='A' && c<='z') || (c==' ')) printf ("%c",c);
       c=mystring[++i];
   }
   printf("\n");
}

======
Werner
0
 
griesshCommented:
>> excluding apostrophe

ooops, ad another "|| (c==0x27)" to the list of exclusions
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
griesshCommented:
Oh well, and since it is C make it:

#include <stdio.h>

int main()

<sigh>
0
 
Sjef BosmanGroupware ConsultantCommented:
0x027 (escape) shoyld be plain 27

You could include <ctype.h>, then you get

#include <ctype.h>
#include <stdio.h>

int main()
{
    char mystring[]="this is wow, hehe... okok.. that's good!";
    char *ptr= mystring;

    printf ("%s\n", mystring);
    for(; *ptr; ptr++)
        if (!ispunct(*ptr))
            putchar(*ptr);
    putchar('\n');
    return 0;
}
0
 
griesshCommented:
"ispunct()" includes the apostrophy and that was an exception :-(

0x27 is the right character, because I wanted to exclude the apostrophy, not the escape

I was thinking about using a pointer, but for the understanding it just makes more sense to work with members of an array.

=====
Werner
0
 
jhshuklaCommented:
editing sjef's code:
for(; *ptr; ptr++)
    if (!ispunct(*ptr) || (*ptr == '\''))
        putchar(*ptr);
putchar('\n');
0
 
ee_guestAuthor Commented:
Thanks all for the input :)
I wish to store the final string in a variable somewhere (not displaying it).

Sorry for all the misunderstanding.
0
 
avizitCommented:
so have another array

char A[Max]   ;  /* Max is maximum length of the string */

so just change sjefs modified  code to
i = 0;
for(; *ptr; ptr++, i++)
    if (!ispunct(*ptr) || (*ptr == '\''))
    A[i] = *ptr;



0
 
ee_guestAuthor Commented:
avizit, thanks for the input..

But i need a function to do that stripping of characters. Because of that, we are not certain of the max size of the array.
0
 
jhshuklaCommented:
char oldStr[] = "whatever";
char *newStr = malloc(sizeof(strlen(oldStr))), *ptr = oldStr;
int i=0;
for(; *ptr; ptr++){
  if(!ispunct(*ptr) || (*ptr != '\''))
    newStr[i++] = *ptr;
}

I used strlen instead of sizeof because oldStr could be char*.
0
 
ee_guestAuthor Commented:
ozo,
can you explain the below?


#include <stdio.h>
#include <ctype.h>
#include <string.h>

void strip_string(char *string);

void main()
{
      char mystring[] = "this is.";

      strip_string(mystring);   /* working */
      strip_string("This is."); /* not working */
      printf("%s\n", mystring);
}

void strip_string(char *string)
{
      char *p0,*p1;
      for ( p0=p1=string; *p1=*p0++; )
      {
            if( !ispunct(*p1) || (*p1 == '\'') )
            {
                  p1++;
            }
      }
}
0
 
griesshCommented:
That's a side effect of ozo's method. strip string is re-using the input string. When you use "This is." as input you pass in a constant which can't be changed.

=====
Werner
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.