user3213110
user3213110

Reputation: 199

C++ Changing lower-case letters in a string function (again...)

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 - ╠╠╠╠.

Upvotes: 1

Views: 1367

Answers (5)

Behrouz.M
Behrouz.M

Reputation: 3573

in c++:

#include <algorithm>
#include <string>
    wstring toLower(const wstring& str)
    {
        wstring temp = str;
        std::transform(temp.begin(), temp.end(), temp.begin(), ::towlower);
        return temp;
    }
    //--------------------------------------------------------------------------------------------------------------------- 
    string toLower(const string& str)
    {
        string temp = str;
        std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
        return temp;
    }

Upvotes: 0

rullof
rullof

Reputation: 7434

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

Upvotes: 1

Wilsonator
Wilsonator

Reputation: 417

If you are allowed you can just ignore the c-style string alpha and use the cctype function toupper(c).

#include <cctype>
for (size_t i = 0; i < strlen(source); i++) {
    source[i] = toupper(source[i]);
}

Upvotes: 0

Paul Draper
Paul Draper

Reputation: 83323

Your switch statement executes every step because you never break or return.

Better:

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

Explanation:

source[i] - 'a' is 0 for 'a', 1 for 'b', etc. It generalizes the pattern you had.

Upvotes: 1

John McMahon
John McMahon

Reputation: 1615

In a switch statement, when a case is matched the code executes all following cases unless you use a break at the end of each case. Your code is always falling through and hitting the default case.

Upvotes: 3

Related Questions