Nizz
Nizz

Reputation: 11

C - Changing a string seems to change unrelated strings in the same way (CS50)

Here is the code:

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>


int main(int argc, string argv[])
{
    string key = argv[1];
    string keyupper = argv[1];
    string keylower = argv[1];


    if (argc != 2) //makes sure there is exactly 2 arguments (the program executable and the key)
    {
        printf("Please input a key.\n");
        return 1;
    }

    else if (strlen(key) != 26) //makes sure the key is exactly 26 letters
    {
        printf("Please make sure the key is 26 unique letters.\n");
        return 1;
    }



    for (int i = 0; i < 26; i++) //the loop to make the uppercase key
    {
        keyupper[i] = toupper(keyupper[i]);
    }



    for (int i = 0; i < 26; i++) //the loop to make the lowercase key
    {
        keylower[i] = tolower(keylower[i]);
    }

Essentially, I want to make a very basic encryption using a key entered while executing the program, it needs to contain 26 unique letters. I want to create two arrays, an uppercase and a lowercase one, to make everything else much easier for me, but when running this code, all keys become either uppercase or lowercase depending on which loop is created last (in this case, they all become lowercase). Even key gets changed to lowercase even though it's used only once as a declaration. Everything else works but this.

This is for the CS50 course so functions such as toupper() are included in libraries.

This is my first ever question so sorry if it's worded poorly. Thank you!

Upvotes: 1

Views: 115

Answers (1)

chux
chux

Reputation: 154090

Code failed to copy the string contents

[Talking about string here, not the type string]

In C, a string is "... is a contiguous sequence of characters terminated by and including the first null character."

Code only copied pointers and not the string contents.

string key = argv[1];
string keyupper = argv[1];
string keylower = argv[1];

Comment discussion indicates OP now sees why code is in error.

Repaired code

//#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

// Avoid naked magic numbers, instead define them
#define KEY_N 26

int main(int argc, string argv[]) {
    // Do not code argv[1] until after argc check
    // string key = argv[1];
    // string keyupper = argv[1];
    // string keylower = argv[1];

    if (argc != 2) {
        printf("Please input a key.\n");
        return 1;
    }

    char *key = argv[1];
    // else if (strlen(key) != 26)letters
    if (strlen(key) != KEY_N) {
        printf("Please make sure the key is 26 unique letters.\n");
        return 1;
    }

    char keyupper[KEY_N + 1];
    char keylower[KEY_N + 1];

    // for (int i = 0; i < 26; i++)
    for (size_t i = 0; i < KEY_N; i++) {
        // keyupper[i] = toupper(keyupper[i]);
        keyupper[i] = toupper((unsigned char) key[i]);
    }
    keyupper[KEY_N] = '\0';
    ...

Upvotes: 1

Related Questions