How to remove and substitute characters within a string

F

francescomoi

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.
 
L

lndresnick

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;
}
 
G

Gregory Pietsch

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
 
P

Peter Nilsson

Gregory said:
...
#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;
 
S

Stan Milam

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 );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top