Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to remove and substitute characters within a string

Reply
Thread Tools

How to remove and substitute characters within a string

 
 
francescomoi@europe.com
Guest
Posts: n/a
 
      04-25-2005
Hi.

I'm trying to remove some characters within a string and substitute
others. For instance, I want to convert:
John's new house, great ---> Johns-new-house-great

I tried with:
----------------//------------------
int main()
{
char string2[50];
char *string = "John's new house, great";
int rc = sscanf(string, "%[, ]%s", string2);
if (rc = 2)
{
printf("%s\n", string2);
}
return 0;
}
-------------//---------------

But it doesn't work. Any suggestion? Thx.

 
Reply With Quote
 
 
 
 
lndresnick@gmail.com
Guest
Posts: n/a
 
      04-25-2005

francesco...@europe.com wrote:
> Hi.
>
> I'm trying to remove some characters within a string and substitute
> others. For instance, I want to convert:
> John's new house, great ---> Johns-new-house-great
>
> I tried with:
> ----------------//------------------
> int main()
> {
> char string2[50];
> char *string = "John's new house, great";
> int rc = sscanf(string, "%[, ]%s", string2);
> if (rc = 2)
> {
> printf("%s\n", string2);
> }
> return 0;
> }
> -------------//---------------
>
> But it doesn't work. Any suggestion? Thx.


Perhaps you want something more like this:

#include <stdio.h>

int main(void)
{
char string2[50];
const char *string = "John's new house, great";
char *p = string2;

for (; *string != '\0'; string++) {
switch(*string) {
case '\'':
case ',':
continue; /* eliminate ' */
case ' ':
*p++ = '-';
break;
default:
*p++ = *string;
break;
}
}

*p = '\0';

printf("%s\n", string2);

return 0;
}

 
Reply With Quote
 
 
 
 
Gregory Pietsch
Guest
Posts: n/a
 
      04-25-2005
A couple of suggestions:

1)

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

int main(void)
{
char *s = "John's new house, great";
char *p = s;

while (*p) {
if (ispunct(*p))
; /* do nothing */
else if (isspace(*p))
putchar('-');
else
putchar(*p);
p++;
}
return 0;
}

2)

Go get FreeDOS edlin, which contains a dynamic string library that has
functions that deal with this type of string-scanning problem.

Gregory Pietsch

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      04-25-2005
Gregory Pietsch wrote:
> ...
> #include <ctype.h>
> #include <stdio.h>
>
> int main(void)
> {
> char *s = "John's new house, great";
> char *p = s;


More robust is...

const char *s = "John's new house, great";
const unsigned char *p = (const unsigned char *) s;

>
> while (*p) {
> if (ispunct(*p))
> ; /* do nothing */
> else if (isspace(*p))
> putchar('-');
> else
> putchar(*p);
> p++;
> }
> return 0;
> }


--
Peter

 
Reply With Quote
 
Stan Milam
Guest
Posts: n/a
 
      04-30-2005
wrote:
> Hi.
>
> I'm trying to remove some characters within a string and substitute
> others. For instance, I want to convert:
> John's new house, great ---> Johns-new-house-great
>
> I tried with:
> ----------------//------------------
> int main()
> {
> char string2[50];
> char *string = "John's new house, great";
> int rc = sscanf(string, "%[, ]%s", string2);
> if (rc = 2)
> {
> printf("%s\n", string2);
> }
> return 0;
> }
> -------------//---------------
>
> But it doesn't work. Any suggestion? Thx.
>


Try this:

/************************************************** ********************/
/* File Id. chrsubst.c. */
/* Author: Stan Milam. */
/* Date Written: 17 Apr. 1992. */
/* Description: */
/* Implement a function to search and replace characters in a C */
/* string. */
/* (c) Copyright 2005 by Stan Milam. */
/* All rights reserved. */
/* */
/************************************************** ********************/

#include <stddef.h>
#include <string.h>

/* $Revision$ */
extern char tb_copyright[];
static char *copyright = tb_copyright;

/************************************************** ********************/
/* Name: */
/* chrsubst(). */
/* */
/* Description: */
/* This function will replace all occurances of one character */
/* with another in a character string. It will then return */
/* the number of substitutions. */
/* */
/* Arguments: */
/* char *s1 - String to be searched. */
/* char ch - Character to search for. */
/* char ch2 - Character to replace the value of ch. */
/* */
/* Returns: */
/* The number of times ch is in s1. */
/* */
/************************************************** ********************/

size_t
chrsubst(char *s1, int ch, int ch2) {

size_t count = 0; /* The count to return */
char *wrk = strchr(s1, ch); /* Find first char in s1 */

while (wrk) { /* While we have matches */
*wrk = (char) ch2; /* Replace the character */
count++, wrk++; /* Increment the count & pointer */
wrk = strchr(wrk, ch); /* Search for next occurance */
}
return count; /* Return the count */
}

/************************************************** ********************/
/* Name: */
/* substitute_char(). */
/* */
/* Description: */
/* Alias of the chrsubst() function with a more english-like */
/* name. */
/* */
/************************************************** ********************/

size_t
substitute_char( char *s, int ch1, int ch2 )
{
return chrsubst( s, ch1, ch2 );
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Remove only special characters and junk characters from a file rvino Perl 0 08-14-2007 07:23 AM
substitute a string within an array? sicario Perl 0 07-30-2007 02:21 PM
how to substitute HTML within <DIV>...</DIV> using XMLHttpRequest ? pamelafluente@libero.it Javascript 7 09-08-2006 08:17 AM
remove a string from within a string soni29 Javascript 2 08-07-2004 03:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57