Solved

Change String

Posted on 2004-09-07
13
197 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
Comment Utility
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
Comment Utility
>> excluding apostrophe

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

Expert Comment

by:griessh
Comment Utility
Oh well, and since it is C make it:

#include <stdio.h>

int main()

<sigh>
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
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
Comment Utility
"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
Comment Utility
editing sjef's code:
for(; *ptr; ptr++)
    if (!ispunct(*ptr) || (*ptr == '\''))
        putchar(*ptr);
putchar('\n');
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:ee_guest
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
char *p0,*p1;
for(p0=p1=mystring; *p1=*p0++;){ if( !ispunct(*p1) || (*p1 == '\'')) ){ p1++; } }
0
 
LVL 9

Expert Comment

by:jhshukla
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now