replace char in string

May 7, 2013 at 10:40pm
Is there a way to replace 3 char's in a string. For example i have a string containing

s = "The school is called 7x8"

I want to replace the "7x8" with "LSU". But the "x" can be any letter or number.

Is there a way to do that ?
May 7, 2013 at 10:49pm
can you post what you have for code so far. its easier to help you if there is a little more context
May 7, 2013 at 10:51pm
sometimes it helps to have code to start with if your stuck. here is an example code for you maybe you can modify it in the way you need
#include <iostream>
#include <ctime>
#include "RandomCharacter.h"
using namespace std;

const int NUMBER_OF_RANDOM_LETTERS = 100;
void createArray(char []);
void displayArray(const char []);
void countLetters(const char [], int []);
void displayCounts(const int []);

int main()
{
// Declare and create an array
char chars[NUMBER_OF_RANDOM_LETTERS];

// Initialize the array with random lowercase letters
createArray(chars);

// Display the array
cout << "The lowercase letters are: " << endl;
displayArray(chars);

// Count the occurrences of each letter
int counts[26];

// Count the occurrences of each letter
countLetters(chars, counts);

// Display counts
cout << "\nThe occurrences of each letter are: " << endl;
displayCounts(counts);

return 0;
}

// Create an array of characters
void createArray(char chars[])
{
// Create lowercase letters randomly and assign
// them to the array
srand(time(0));
for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
chars[i] = getRandomLowerCaseLetter();
}

// Display the array of characters
void displayArray(const char chars[])
{
// Display the characters in the array 20 on each line
for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
{
if ((i + 1) % 20 == 0)
cout << chars[i] << " " << endl;
else
cout << chars[i] << " ";
}
}

// Count the occurrences of each letter
void countLetters(const char chars[], int counts[])
{
// Initialize the array
for (int i = 0; i < 26; i++)
counts[i] = 0;

// For each lowercase letter in the array, count it
for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
counts[chars[i] - 'a'] ++;
}

// Display counts
void displayCounts(const int counts[])
{
for (int i = 0; i < 26; i++)
{
if ((i + 1) % 10 == 0)
cout << counts[i] << " " << static_cast<char>(i + 'a') << endl;
else
cout << counts[i] << " " << static_cast<char>(i + 'a') << " ";
}
}
May 7, 2013 at 11:02pm
I suggest you look at the algorithm library, it has many useful functions that work on pretty much every standard container.

1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <string>


int main()
{
    std::string a("The school is called 7x8");
    std::string b("LSU");
    std::copy(b.begin(), b.end(), a.begin() + a.find('7'));
    std::cout << a << std::endl;
    return 0;
}
May 7, 2013 at 11:11pm
Ill take a look at that thank you.
May 8, 2013 at 1:30am
That worked perfectly ! Thank you ! How can i make it replace "7x8" in the whole string if it appears more than once ?

Example :

s = "The school "7x8" is a nice place. "7x8" is very popular.
Last edited on May 8, 2013 at 1:32am
May 8, 2013 at 5:52am
Store all locations of "7x8" in the string, the copy LSU to each of those locations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>

int main()
{
    std::string a("The school 7x8 is a nice place. 7x8 is very popular.");
    std::string b("LSU");
    std::vector<int> locations;
    for (int i = 0; i < a.size(); ++i)
    {
        locations.push_back(a.find("7x8", i));
    }
    for (int i = 0; i < locations.size(); ++i)
    {
        std::copy(b.begin(), b.end(), a.begin() + locations[i]);
    }
    std::cout << a << std::endl;
    return 0;
}
Topic archived. No new replies allowed.