473,466 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Algorithm for character replacement in string

I have two strings of corresponding characters, for example:
s1 = "abc"
s2 = "XYZ"

Is there an algorithm so that whenever a character from S1 occurs
in a string, it is replaced by the corresponding character from S2?

eg. running "crabs" through this function will producs "ZrXYs".

Jun 21 '06 #1
3 3101
Old Wolf wrote:
I have two strings of corresponding characters, for example:
s1 = "abc"
s2 = "XYZ"

Is there an algorithm so that whenever a character from S1 occurs
in a string, it is replaced by the corresponding character from S2?

eg. running "crabs" through this function will producs "ZrXYs".


Do you mean, as a single function call, in the Standard library? No.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 21 '06 #2
Old Wolf posted:
I have two strings of corresponding characters, for example:
s1 = "abc"
s2 = "XYZ"

Is there an algorithm so that whenever a character from S1 occurs
in a string, it is replaced by the corresponding character from S2?

eg. running "crabs" through this function will producs "ZrXYs".

I'm doing this just for the fun of it. If it's your homework, then in the
long run, you won't gain from copy-pasting my code... all it takes is one
exam to demonstrate your inability.
#include <cassert>

#ifndef NDEBUG

#include <cstring>

#endif

void SmartReplace( char *p,
const char * const check_for,
const char * const replace_with )
{

assert( std::strlen(replace_with) >= std::strlen(check_for) );

for( ; *p; ++p )
{
const char *p_check = check_for;
const char *p_replace = replace_with;

for( ; *p_check; ++p_check, ++p_replace )
{
if ( *p == *p_check ) *p = *p_replace;
}
}
}

#include <iostream>

#include <cstdlib>

int main()
{
using std::cout;

char str[] = "crabs";

SmartReplace( str, "abc", "XYZ" );

cout << str << '\n';

std::system("PAUSE");
}
--

Frederick Gotham
Jun 21 '06 #3
* Old Wolf:
I have two strings of corresponding characters, for example:
s1 = "abc"
s2 = "XYZ"

Is there an algorithm so that whenever a character from S1 occurs
in a string, it is replaced by the corresponding character from S2?

eg. running "crabs" through this function will producs "ZrXYs".


On a system where char is 8-bit or thereabouts, use an array of all
possible character values as indices, with replacements as values. For
a more portable solution use a std::map<char,char>. Then apply
std::transform, or just use a for loop.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two...
2
by: Phillip Hamlyn | last post by:
I'm trying to get a Yenc Decoding algorithm working but keep getting the same problem. I'm using data from the yenc test newsgroup and have tried the same thing with most of the example source code...
3
by: jose luis fernandez diaz | last post by:
Hi, Does any know a algorithm to strip heading and leading blank in a string ? Tnansk, Jose Luis
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
2
by: GRB | last post by:
A client wants me to decrypt a cookie. They encrypted the data in java using TripleDES. The key provided is a 168 bit 44 character key, DESede alogorithm. Ive tried to decrypt in vb.net and get the...
10
by: Nemok | last post by:
Hi, I am trying to write an additive encryption algorithm in C++ that will encrypt a text by adding a random numer to each character in a string. The code looks similar to this: for(int...
6
by: aarklon | last post by:
Hi folks, I found an algorithm for calculating the day of the week here:- http://www.faqs.org/faqs/calendars/faq/part1/index.html in the section titled 2.5 what day of the week was 2 august...
7
by: Tom de Neef | last post by:
I need to change one character at a known position in a string. In Pascal I would change the P's character of a string S into 'x' with S:='x'; In JavaScript I come no further than S =...
5
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.