C ++在字符串函数中更改小写字母(再次...) [英] C++ Changing lower-case letters in a string function (again...)

查看:139
本文介绍了C ++在字符串函数中更改小写字母(再次...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

又是我。在我所有的问题中,我认为这是他们中最愚蠢的一件事,但无论是因为疲劳还是愚蠢,我都需要一些帮助。然而,最重要的是,我正在为我的任务做这件事,并且有一条严格的规则 - 我必须使用一个名为

的函数 b
$ b $ p codehar $ encode(char * source,char const * alpha)



这是我非常原始的代码块:

  int len = strlen(source); 
switch(source [i])
{
case'a':source [
for(int i = 0; i< len; i ++) i] = alpha [0];
case'b':source [i] = alpha [1];
case'c':source [i] = alpha [2];
................................
.......... ......................
........................ ........
case'y':source [i] = alpha [24];
case'z':source [i] = alpha [25];
default:source [i] = source [i];
}
}
cout<<源<< ENDL;

基本上应该输入一个字符串 source 不超过1000个符号将其所有小写符号('a' - 'z')更改为已输入数组的相应符号(每个小写字母总共26个符号...a发生变化有一个[0],'b'和b [1]等)。

这里有几个问题:


  1. 我的代码不起作用。输出总是一些奇怪的符号。我该如何解决这个问题?

  2. 我该如何缩短它?也许使用作为语句而不是 switch

  3. 我怎么才能将它实现到我在问题开始时提到的功能(这可能是最重要的功能)?

只是为了记录,这里是我的输入代码:

  char source [1001]; 
cin.getline(source,1001,'/ n');

char alpha [27];
cin.getline(alpha,27);

编辑:我将我的代码更改为:

  #include< iostream> 
#include< string>
使用namespace std;

int main()
{
char source [1001];
cin.getline(source,1001,'/ n');

char alpha [27];
cin.getline(alpha,27);

const int len = strlen(source);如果('a'< = source [i]&& source [i]< = b>,则为
为(int i = 0; i< len; i ++) 'z')
{
source [i] = alpha [source [i] - 'a'];
}
}
cout<<源<< ENDL;
返回0;
}

然而它变得越来越笨重。现在我的控制台输入永远不会结束....逐字...单击Enter不会停止它..没有任何影响..当我将 cin.getline 更改为 source 10 它以某种方式结束,但又返回了那些奇怪的符号 - ╠╠╠╠

解决方案使用 cstring 来代替 string

  #include< cstring> 

用<$ c替换开关 $ c> if(source [i]> ='a'&& source [i]< ='z')...



这是您的最终代码: https://eval.in/95037

  #include< iostream> 
#include< cstring>

使用namespace std;

编码(char * source,const char * alpha)
{
int i,j;
int len = strlen(source); (source [i]> ='a'&& source [i]< ='z'){
for(i = 0; i< len; i ++)
j = source [i] - 'a';
source [i] = alpha [j];



$ b $ int main(void)
{
char source [1001];
char alpha [27];
cin.getline(source,1000);
cin.getline(alpha,27);
编码(来源,alpha);
cout<<源<< ENDL;
返回0;
}


It's me again. Out of all my questions I think this is the dumbest of them all but either due to fatigue or stupidity I need some help on this one as well. The most important thing, however, is that I'm doing this for an assignment of mine and there's one strict rule - I MUST use a function called

char* encode(char* source, char const* alpha)

Here's my piece of very primitive code:

    int len = strlen(source);
    for (int i = 0; i < len; i++)
    {
        switch (source[i])
        {
        case 'a': source[i] = alpha[0];
        case 'b': source[i] = alpha[1];
        case 'c': source[i] = alpha[2];
        ................................
        ................................
        ................................
        case 'y': source[i] = alpha[24];
        case 'z': source[i] = alpha[25];
        default: source[i] = source[i];
        }
    }
    cout << source << endl;

It basically should make an inputted string source of no more than 1000 symbols change all of its lower-case symbols ('a' - 'z') to the corresponding symbol of the already inputted array (26 symbols in total for each lower-case letter... 'a' changes with a[0], 'b' with b[1], etc.).

There are several problems I have here:

  1. My code won't work.. The output is always some weird symbols. How can i fix that?
  2. How can I shorten it? Perhaps use a for statement instead of switch?
  3. When it's working, how can I implement it to the function I mentioned in the beginning of my question (this is probably the most important one)?

Just for the record, here is my input code as well:

char source[1001];
cin.getline(source, 1001, '/n');

char alpha[27];
cin.getline(alpha, 27);

EDIT: I changed my code to:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char source[1001];
    cin.getline(source, 1001, '/n');

    char alpha[27];
    cin.getline(alpha, 27);

    const int len = strlen(source);
    for (int i = 0; i < len; i++)
    {
        if ('a' <= source[i] && source[i] <= 'z')
        {
            source[i] = alpha[source[i] - 'a'];
        }
    }
    cout << source << endl;
    return 0;
}

Yet it became buggier. Now my console input never ends.... literally.. clicking Enter doesn't stop it.. nothing does.. When I changed my cin.getline for source to 10 it somehow ended though returned those weird symbols again - ╠╠╠╠.

解决方案

Use cstring instead of string:

#include <cstring>

And replace the switch with if (source[i] >= 'a' && source[i] <= 'z') ...

And this is your final code: https://eval.in/95037

#include <iostream>
#include <cstring>

using namespace std;

void encode(char *source, const char *alpha)
{
    int i, j;
    int len = strlen(source);
    for (i = 0; i < len; i++) {
        if (source[i] >= 'a' && source[i] <= 'z') {
            j = source[i] - 'a';
            source[i] = alpha[j];
        }
    }
}

int main(void)
{
    char source[1001];
    char alpha[27];
    cin.getline(source, 1000);
    cin.getline(alpha, 27);
    encode(source, alpha);
    cout << source << endl;
    return 0;
}

这篇关于C ++在字符串函数中更改小写字母(再次...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆