Home > Blockchain >  String program(replace letter)
String program(replace letter)

Time:03-24

I need your help with a program in Turbo C .

I need to create a program using a string. Write any sentence, and in the sentence I need to find the first b or B letter then write cb before it.

For example:

acdcb -> acdccbb

I tried to do it, but can do it only like a replace. I just started to learn strings, so I have such code:

#include <iostream.h>
#include <conio.h>
#include <string.h>

int main()
{
    clrscr();
    const int N=100;
    char m[N];
    char r[]="cb";
    int p=0,kb=0;
    cout<<"Sentence: ";
    cin>>m;
    p=strlen(m);
    cout<<"String length = "<<p<<"\n";
    for(int i=0;i<p;i  )
    {
        if(m[i]=='b'||m[i]=='B')
        {
            kb  ;
            if(kb==1)
            {
                m[i-1]='b';
                m[i-2]='c';
            }
        }
    }
    cout<<"New Sentence : "<<m;
    p=strlen(m);
    cout<<"\ncount of letters = "<<p;
    getch();
}

image

CodePudding user response:

You are only replacing existing characters, you are not shifting any characters around to make room for the new characters.

So, for example, if you enter dasdasB, m will look like this:

----------------------------------
| d | a | s | d | a | s | B | \0 |
----------------------------------

What you are doing is simply replacing the 2nd a with c, and the 2nd s with b:

----------------------------------
| d | a | s | d | c | b | B | \0 |
----------------------------------
                  ^   ^   ^
                 i-2 i-1  i

Instead, you need to shift all of the characters including and after B to the right 2 spaces:

------------------------------------------
| d | a | s | d | a | s | --|-> | B | \0 |
------------------------------------------
                          ^   ^

And then you can fill in the empty spaces you just created:

------------------------------------------
| d | a | s | d | a | s | c | b | B | \0 |
------------------------------------------
                          ^   ^

I'll leave that as an exercise for you to figure out how to do that shifting.

Hint: use another loop that starts at the end of the string (you already know the string's length), moving characters to the right 2 spaces, looping backwards until it reaches the b/B character at index i.


That being said, this code would be a lot easier if you were using std::string instead of a char[] array, as std::string has find_first_of() and insert() methods, eg:

#include <iostream>
#include <string>
//#include <conio.h>
using namespace std;

int main()
{
    //clrscr();
    string m;
    cout << "Sentence: ";
    cin >> m;
    cout << "String length = " << m.size() << "\n";
    string::size_type i = m.find_first_of("bB");
    if (i != string::npos) {
        m.insert(i, "cb");
    }
    cout << "New Sentence : " << m << "\n";
    cout << "count of letters = " << m.size();
    cin.get();
}

Online Demo

CodePudding user response:

You need to create a new array with size 3 greater than m. 2 characters will be occupied by c and b and last one would be '\0' indicating the end of char array. I have a code which can be helpful for you. It adds two more characters of c and b before the b character.

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    const int N=100;
    char m[N];
    char r[]="cb";
    int p=0,kb=0;
    cout<<"Sentence: ";
    cin>>m;
    p=strlen(m);
    cout<<"String length = "<<p<<"\n";
    char *result = new char [p 3];
    for(int i=0;i<p;i  )
    {
        if(m[i]=='b'||m[i]=='B')
        {
            kb  ;
            if(kb==1)
            {
                for (int j=0; j<i; j  )
                {
                    result[j] = m[j];
                }
                result[i]='c';
                result[i 1]='b';
                for (int j=i 2, k=i; k<p; j  , k  )
                {
                    result[j] = m[k];
                }
                result[p 2] = '\0';
            }
        }
    }
    cout<<"New Sentence : "<<result;
    p=strlen(m);
    cout<<"\ncount of letters = "<<p;
}
  • Related