0

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    Side note: Turbo C++ is Ooooooold. There are lessons to be taken from C++ that can still be applied today, but the language itself has been obsolete for more than 20 years and C++ has moved on. If this is for a course, do what you have to do to pass the course, but you owe it to yourself to learn from [some up-to-date materials](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on modern C++ before you go looking for a programming job. – user4581301 Mar 23 '22 at 21:29
  • 2
    Side note: `cin>>m;` stops when it hits the first whitespace character, so it it better described as reading words, not sentences. – user4581301 Mar 23 '22 at 21:31
  • `std::string s; ... for (int i = 0; i < p; ++i) { if ( m[i] == 'B' || m[i] == 'b' ) s += "cb"; s += m[i]; } std::cout << s;`-- It's that simple **if** you would have used C++ as it has been since 1998. – PaulMcKenzie Mar 23 '22 at 21:49
  • @user4581301 -- It seems to be a "tradition" in certain areas of the world to give students Turbo C++ to use. The problem is that the student thinks that they're learning C++ as it stands now, and not knowing that what they're learning has little to no relevance in the actual C++ world. What is surprising is that the compilers the student could use that are modern and up-to-date are totally free-of-charge. I could understand if the newer compilers cost hundreds or thousands of dollars, but that isn't the case. There really is no excuse to dump Turbo C++ on any student. – PaulMcKenzie Mar 23 '22 at 21:54
  • @PaulMcKenzie the built-in IDE and rapid compile/run cycle makes Turbo C++ a useful learning tool, even if it's teaching you the wrong thing. Plus there's the matter of updating class materials if you were to change it. – Mark Ransom Mar 24 '22 at 00:52

2 Answers2

2

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • What i can do, if first letter B/b dont will be last letter? – Speak Dony Mar 23 '22 at 21:44
  • It doesn't matter if the B/b is the last character in the string or not. Move ALL characters INCLUDING AND FOLLOWING the B/b, until you make space for the new characters you want to insert. – Remy Lebeau Mar 23 '22 at 21:53
0

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;
}
Daniyal Ishfaq
  • 247
  • 1
  • 7
  • `char result[p+3];` Not legal C++ code. Some compilers, notably GCC, add Variable Length Arrays (VLA) as an extension and those that do generally follow the same rules as per C99's VLA, but for C++ compiler that do not support VLA, your answer cannot be used. I haven't used Turbo C++ for about 30 years, but I strongly suspect it does not support VLA. Nor does it support `std::vector`, the recommended replacement. That forces the asker to use `new[]`/`delete[]` to get a dynamic array. – user4581301 Mar 23 '22 at 22:05
  • Yes you are right, we need to use new to allocate memory. I am gonna update my answer. Thank you for pointing it out. – Daniyal Ishfaq Mar 23 '22 at 22:06
  • In addition it seems the asker has been tasked with the more complicated job of performing inserts into the existing array. Otherwise I'd forget about the second array entirely and output the updated string directly to the output stream character by character, writing out the inserted characters before writing the character that triggers them. – user4581301 Mar 23 '22 at 22:06
  • Guys, ty for answer. Idk, maybe for me the question was so hard or just im so bad in that. Finally, program created by me - how much it bad? Sorry for my English. Because i studying at 2 course of college for programming, but it in UA(It's like 11 class). I lost for that program 3 hours of life but don't create it finally – Speak Dony Mar 23 '22 at 22:21
  • Don't worry man. Everyone has to start somewhere. Hopefully, in the future you'll get better at it if you keep practicing. – Daniyal Ishfaq Mar 23 '22 at 22:24
  • The real shame, @SpeakDony , is there isn't much we can point you at to help you learn. The language dialect used by Turbo C++ was dying out before the Internet really got going. What little made it up onto the Internet is long gone. – user4581301 Mar 23 '22 at 23:41