473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to remove and substitute characters within a string

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
4 9297

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: qwweeeit | last post by:
For a python code I am writing I need to remove all strings definitions from source and substitute them with a place-holder. To make clearer: line 45 sVar="this is the string assigned to sVar"...
5
by: jjliu | last post by:
Could someone tell me how to remove all html tags (and anything inside tags) by perl. Some people suggested me to use HTML::TagFilter but i could not find window version. Thanks very much for your...
11
by: deko | last post by:
I need to loop through a string and remove all characters except numbers or letters. I am getting an ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the...
5
by: lazypig06 | last post by:
Hi ! I am a PHP beginner. I hope somebody can help me with this problem that I've been having. I've been trying to clean up junk data that I have at the begining and ending of an xml file....
8
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? à è ì ò ù À È Ì Ò Ù á é í ó ú ý Á É Í Ó Ú Ý â ê î ô û Â Ê Î Ô...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
9
by: Smiley | last post by:
Hi, Can someone tell me how to remove aspostophe (') from user input. I don't want to give any error message. Just want to remove or change it to "" or null, such input when it appear anywhere...
10
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.