Solved

Change String

Posted on 2004-09-07
13
203 Views
Last Modified: 2010-04-15
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
Comment
Question by:ee_guest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 11999107
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
 
LVL 11

Expert Comment

by:griessh
ID: 11999159
>> excluding apostrophe

ooops, ad another "|| (c==0x27)" to the list of exclusions
0
 
LVL 11

Expert Comment

by:griessh
ID: 11999196
Oh well, and since it is C make it:

#include <stdio.h>

int main()

<sigh>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11999729
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
 
LVL 11

Expert Comment

by:griessh
ID: 11999914
"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
 
LVL 9

Expert Comment

by:jhshukla
ID: 12002433
editing sjef's code:
for(; *ptr; ptr++)
    if (!ispunct(*ptr) || (*ptr == '\''))
        putchar(*ptr);
putchar('\n');
0
 

Author Comment

by:ee_guest
ID: 12002445
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
 
LVL 11

Expert Comment

by:avizit
ID: 12003287
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
 

Author Comment

by:ee_guest
ID: 12003406
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
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 12003424
char *p0,*p1;
for(p0=p1=mystring; *p1=*p0++;){ if( !ispunct(*p1) || (*p1 == '\'')) ){ p1++; } }
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12006385
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
 

Author Comment

by:ee_guest
ID: 12006828
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
 
LVL 11

Expert Comment

by:griessh
ID: 12007346
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question