• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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.
0
ee_guest
Asked:
ee_guest
  • 5
  • 3
  • 2
  • +3
1 Solution
 
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
 
griesshCommented:
Oh well, and since it is C make it:

#include <stdio.h>

int main()

<sigh>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
ozoCommented:
char *p0,*p1;
for(p0=p1=mystring; *p1=*p0++;){ if( !ispunct(*p1) || (*p1 == '\'')) ){ p1++; } }
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now