Replace a character of a string and show the final string with spaces included

3

Basically my code is ready, the only problem is that when I show the modified end string, it only shows me the first word (When it finds a space it ignores the rest) I know that my program stops counting when it meets "\ 0" and I also know that the space counts as "\ 0" in addition to the ENTER.

I would like to know some solution to this problem, I am new to this c ++ and I know that my mistake must be silly. This is my code:

void reemplazo(char *v, char c1, char c2)

{

    int i;

    for (i=0;v[i]!='
void reemplazo(char *v, char c1, char c2)

{

    int i;

    for (i=0;v[i]!='%pre%';i++)
    {
        if (*(v+i)==c1)
        {
            *(v+i)=c2;
        }
    }

}

int main ()

{

    char palabra[20];
    char a,b;
    int i;

    cout << "Ingrese palabra: ";
    cin >> palabra;
    fflush(stdin);
    cout << "Ingrese caracter a reemplazar: ";
    cin >> a;
    cout << "Ingrese nuevo caracter: ";
    cin >> b;
    reemplazo(palabra,a,b);
    cout <<"resultado: ";

    for (i=0;i<20;i++)
    {
         cout<<palabra[i];
    }

    return 0;
}
';i++) { if (*(v+i)==c1) { *(v+i)=c2; } } } int main () { char palabra[20]; char a,b; int i; cout << "Ingrese palabra: "; cin >> palabra; fflush(stdin); cout << "Ingrese caracter a reemplazar: "; cin >> a; cout << "Ingrese nuevo caracter: "; cin >> b; reemplazo(palabra,a,b); cout <<"resultado: "; for (i=0;i<20;i++) { cout<<palabra[i]; } return 0; }
    
asked by FranciscoFJM 24.03.2017 в 17:07
source

2 answers

3

As you mentioned, the problem is that you are only reading a word directly. The following instruction (link in English) should solve the problem:

cout << "Ingrese palabra: ";
cin.getline(palabra, 20);

And I would remove the instruction:

fflush(stdin);

fflush can only be used with output streams, such as stdout , use with stdin is undefined , and although some compilers may work, its use is not indicated as well.

    
answered by 24.03.2017 / 17:35
source
2

programming in C ++ it would be more useful to stop using functions or C structures to make the program simpler and easier. For example, using strings , passage by reference (with & ), and abstract with cin or cout that are responsible for escaping the characters that are bringing you problems.

Here I leave you what the program in C ++ would be like using everything I mentioned:

#include<bits/stdc++.h>
using namespace std;

void reemplazo(string &v, char c1, char c2)

{
    int i;
    for (i = 0; i < v.length(); i++)
    {
        if (v[i] == c1)
        {
            v[i] = c2;
        }
    }

}

int main ()

{

    string palabra;
    char a,b;
    int i;

    cout << "Ingrese palabra: ";
    cin >> palabra;
    cout << "Ingrese caracter a reemplazar: ";
    cin >> a;
    cout << "Ingrese nuevo caracter: ";
    cin >> b;
    reemplazo(palabra,a,b);
    cout <<"resultado: " << palabra << endl;

    return 0;
}
    
answered by 24.03.2017 в 18:08