创建一个单词移位器

时间:2014-08-28 17:05:02

标签: c++

对于作业,我正在使用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;

3 个答案:

答案 0 :(得分:1)

这应该有用。

 // 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;
  • 更新:使letter无符号,因此溢出检查有效。

答案 1 :(得分:1)

考虑到你的语法,我为你写了一个例子。除此之外,它是传统的 在相关代码之前写评论。

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

答案 2 :(得分:0)

尝试并调​​查此代码

#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符号。