创建单词移位器

Creating a word shifter

本文关键字:移位器 单词 创建      更新时间:2023-10-16

对于一个赋值,我正在C++中创建一个单词移位器。我对C++几乎没有经验,所以这很难。我想我真的很接近,但只是缺少了C++的一些语法。如有任何帮助,我们将不胜感激。

string s = phrase;
int length = s.length();
//find length of input to create a new string
string new_phrase[length];
//create a new string that will be filled by my for loop
for (int i=0; i<length; i++) 
//for loop to go through and change the letter from the original to the new and then            put into a string
{
    int letter = int(s[i]); 
    int new_phrase[i] = letter + shift;
//this is where I am coming up with an error saying that new_phrase is not initialized
    if (new_phrase[i] > 122)
//make sure that it goes back to a if shifting past z
    { 
        new_phrase[i] = new_phrase[i] - 26;
    }
}
cout << new_phrase<< endl;

考虑到您的语法,我为您编写了一个示例。此外,它是传统的在相关代码之前编写注释。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    //test value;
    int shift = 3;
    string s = "hello string";
    //find length of input to create a new string
    int length = s.length();
    //create a new string.it's length is same as 's' and initialized with ' ';
    string new_phrase(length, ' ');
    for (int i=0; i<length; i++) 
    {
        //no need to cast explicitly.It will be done implicitly.
        int letter = s[i]; 
        //It's assignment, not declaration
        new_phrase[i] = letter + shift;
        //'z' is equal to 126.but it's more readable
        if (new_phrase[i] > 'z')
        { 
            new_phrase[i] = new_phrase[i] - ('z' - 'a' + 1);
        }
    }
    cout << new_phrase<< endl;
}

这应该可以工作。

 // must be unsigned char for overflow checking to work.
 char Shifter(unsigned char letter)
 {
       letter = letter + shift;
       if (letter > 'z')
           letter = letter - 26;
       return letter;
 }
// :
// :
 string new_phrase = phrase;   // mainly just allocating a string the same size.
 // Step throught each char in phrase, preform Shifter on the char, then
 // store the result in new_phrase.
 std::transform(phrase.begin(), phrase.end(), new_phrase.begin(), Shifter);
 cout << new_phrase<< endl;
  • UPDATE:使letter无符号,因此溢出检查有效

尝试并调查此代码

#include <iostream>
#include <string>
#include <cctype>
void ShiftRight( std::string &s, std::string::size_type n )
{
    if ( n >= 'Z' - 'A' + 1 ) return;
    for ( char &c : s )
    {
        bool lower_case = std::islower( c );
        c = std::toupper( c );
        c = ( c + n -'A' ) % ('Z' -'A' + 1 ) + 'A';
        if ( lower_case ) c = std::tolower( c );        
    }
}
int main() 
{
    std::string s( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
    std::cout << s << std::endl << std::endl;
    for ( std::string::size_type i = 1; i <= 'Z' -'A' + 1; i++ )
    {
        std::str    std::string s( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
        ShiftRight( s, i );
        std::cout << s << std::endl;
    }
    return 0;
}

输出为

ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG
IJKLMNOPQRSTUVWXYZABCDEFGH
JKLMNOPQRSTUVWXYZABCDEFGHI
KLMNOPQRSTUVWXYZABCDEFGHIJ
LMNOPQRSTUVWXYZABCDEFGHIJK
MNOPQRSTUVWXYZABCDEFGHIJKL
NOPQRSTUVWXYZABCDEFGHIJKLM
OPQRSTUVWXYZABCDEFGHIJKLMN
PQRSTUVWXYZABCDEFGHIJKLMNO
QRSTUVWXYZABCDEFGHIJKLMNOP
RSTUVWXYZABCDEFGHIJKLMNOPQ
STUVWXYZABCDEFGHIJKLMNOPQR
TUVWXYZABCDEFGHIJKLMNOPQRS
UVWXYZABCDEFGHIJKLMNOPQRST
VWXYZABCDEFGHIJKLMNOPQRSTU
WXYZABCDEFGHIJKLMNOPQRSTUV
XYZABCDEFGHIJKLMNOPQRSTUVW
YZABCDEFGHIJKLMNOPQRSTUVWX
ZABCDEFGHIJKLMNOPQRSTUVWXY
ABCDEFGHIJKLMNOPQRSTUVWXYZ

至于你的代码,那当然是错误的。您不必定义字符串数组。不要像122那样使用幻数。此外,您可以在我的代码中检查下一个符号是否为alpha符号。