Solved

Change String

Posted on 2004-09-07
13
200 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 163
Why is compiler in oracle server ? 9 66
Resolve Dependency Issues 4 67
NEED HELP WITH VISUAL STUDIO 2017 (beginner) 6 39
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

773 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