字符串程序(替换字母)

发布于 2025-01-16 09:40:28 字数 1052 浏览 2 评论 0 原文

我需要你关于 Turbo C++ 程序的帮助。

我需要使用字符串创建一个程序。写任何句子,在句子中我需要找到第一个 bB 字母,然后在它前面写上 cb

例如:

acdcb -> acdccbb

我尝试这样做,但只能像替换一样做到。我刚刚开始学习字符串,所以我有这样的代码:

#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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缘字诀 2025-01-23 09:40:28

您只是替换现有角色,而不是移动任何角色来为新角色腾出空间。

因此,例如,如果您输入 dasdasBm 将如下所示:

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

您所做的只是替换第二个 a c,第二个 sb

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

相反,您需要转移所有右侧包括 B 在内的字符2 个空格:

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

然后您可以填写刚刚创建的空白区域:

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

我将把它作为练习,让您弄清楚如何进行这种转换。

提示:使用另一个从字符串末尾开始的循环(您已经知道字符串的长度),将字符向右移动 2 个空格,向后循环,直到它到达索引 i 处的 b/B 字符。


话虽这么说,如果您使用 std::string 而不是 char[] 数组,这段代码会容易得多,如 std::string< /code> 有 find_first_of()insert() 方法,例如:

#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();
}

在线演示

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

海未深 2025-01-23 09:40:28

您需要创建一个大小为 3 且大于 m 的新数组。 c 和 b 将占用 2 个字符,最后一个字符为“\0”,表示 char 数组的结尾。我有一个代码,可以对您有所帮助。它在 b 字符之前添加了 c 和 b 两个字符。

#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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文