By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,919 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,919 IT Pros & Developers. It's quick & easy.

How to remove and substitute characters within a string

P: n/a
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.

Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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

Nov 14 '05 #2

P: n/a
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

Nov 14 '05 #3

P: n/a
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

Nov 14 '05 #4

P: n/a
fr**********@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.


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 );
}
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.