3

When encrypt[n] is declared, it doesn't allow for the end of string marker at the end. Also, the end of string marker is never added to the string, so when it is printed at the end of the program, garbage data following the string is also printed, until it finds random data that looks like the end of string marker. That answers your question, but there are ...


2

It's not so much being an idiot as it is having tunnel vision. ;-) The problem isn't about vigKey. It's about what was done with the key string. If you look carefully at what the working code does, you'll see that the key is transformed to all lower case, so that it can be processed by subtracting 'a'. In the second version, the key isn't converted to all ...


1

The detail you seem to miss is that a char array is not a string unless it ends in the NUL char '\0' If you have char text[] = "Foobar";, that is stored {'F', 'o', 'o', 'b', 'a', 'r', '\0'} Without that final NUL char, if you were to printf("%s\n", text);, printf would only stop printing when it found a NUL, which may not necessarily be after the 'r' in ...


1

Let's start here: if (argc != 2 && !isalpha(argv[1])) BOTH conditions have to be met for the usage statement to be printed. The problem is that if either condition is met, it should print out the usage statement. Also, you're still trying to check a string with isalpha when it can only handle one char at a time. Next is this: for (int i = 0;...


1

There are a number of issues in the code that are impacting it. First, look at this: int *keyInt = malloc(strlen(key)); This allocates n bytes where n is the number of chars in the key string. However, the pointer keyInt is a pointer to a single integer. Even with that, a char is one byte, an int is 4 bytes, so if you're converting a char to an int, ...


1

When do you want this printf("%c", text[i]); to execute? When does it actually execute? Spoiler: once for every char in text. That explains the "doubling". I will also warn that this key_val = key[i%text_length]; is going to cause heartbreak. The key is not "attached" to the text either by length or by index. Remember you don't "use" a k character when the ...


1

Maybe it gets clearer if you use 'A' instead of 65 (char is a kind of integer, so 'A' is an 8-bit integer of value 65). In case of a lowercase letter, subtract 'a' (or 97) instead of 'A' (or 65). And in the last step add the one that matches the case of the plaintext letter, not the key letter. You could reduce your cases from four combinations to two by ...


1

Look at where and how k is declared. During each pass through the for loop, k is declared as starting at argv[i]. That means that on the first pass, k=argv[0], the name of the program. This sneaks past, as does the next pass. On the third pass, string k=argv[2], which doesn't exist, so it fails. Remember, argv[x] is a string in the argv array. argv[1][x]...


1

The code is actually testing for non-alpha keys. The problem is that it is prompting for the plaintext before checking the key. It's a sequence issue. As a side issue, consider how you might make the code more efficient. For example, the code is structured like this: if(argc==2) { //the bulk of the program } else if(argc!=2) { printf("Usage: ./...


1

atoi takes a string as argument. You are passing it a single char. As an example, if you have atoi("123"); you'll get 123. But you can't pass it atoi('1') as that is a char. If you are trying to get the ascii value of each char in the keyword, you can subtract either 'A' or 'a' from the char, such that if argv[1][a] was 'c', then 'c' - 'a' is 2. I ...


1

I really don't know where your numbers start and end, and what they refer to (I don't think that's the output of the code you show), at least some whitespace between would have been nice. Also, the non-alpha part seems to work fine. You need to apply some wrap-around. If shifting a 'Z' further forward, it should become 'A' again. You can achieve this by ...


1

Any non-letter should be returned as-is, not changed at all. Note that copying a non-letter won't affect your position inside the keyword! plaintext: Meet me at the park at eleven am effective key: baco nb ac onb acon ba conbac on ciphertext: Negh zf av huf pcfx bt gzrwep oz


1

The index inside the keyword should be incremented only when encrypting a letter. It has to be kept separate from the index in the plaintext, which advances on any character. Case of character to be encrypted is independent of case in keyword. For simplicity, you could ignore the case in the keyword by writing kc = tolower(kc) - 97; or similar.


1

Don't be scared by the number of errors, fixing them one by one quickly reduces the number. If you are unsure about how to use a feature, search it in the CS50 reference or on the internet. It's really annoying to see people like you not doing their search first. First error is about tolower not accepting strings. When you see something like that, look it ...


1

isalpha() takes a single character as a parameter. You are trying to shove an entire string down its throat, so it chokes on it and gives you back a seg fault. If you want to check the string, you need to loop through argv[1] and check each char individually. If this answers your question, please click on the check mark to accept. Let's keep up on forum ...


1

I see a problem here. After you type condition and it is more than one line you need those brackets: if (!isalpha(argv[1][i])) { printf("Please enter ...\n"); return 1; } Otherwise the program will execute return 1 in any case, not caring about the condition. Hope this helps! If yes, please tick the check mark to accept it as ...


1

printf("%c", ((((text[j] - 65) + key[p]%strlen(key))% 26) + 65)); The problem arises because of how strlen() was used here. strlen() returns a value of type size_t, which is actually an unsigned long. This is implicitly casting the overall result as an unsigned long as well. The compiler looks at this and sees that it's trying to insert it into a %c, and it ...


1

I'm not entirely sure, but I'm thinking that your problem lies here: if (c > kl - 1) { key[c] = (key[c % kl]); } This attempts to do an assignment to an element in key[]. Let's say that kl=5 and c=5, and key was defined as having 5 elements from 0 to 4. That means that you're doing this: if (5 > 5 - 1) { key[5] = (key[0]); } This means that you'...


1

I suspect that you are encoding characters, but incorrectly. That's because you are selecting the wrong char from the key to encode. Try working through the code with paper and pencil and sample data to convince yourself. Your formula for which character to use in the key string is incorrect. The index for your key, j, is completely independent from i, ...


1

int x = (argv[1][j])-'0'; looks like your problem. You are subtracting the ascii value of the 0 symbol from the character in argv[1][j]. In effect, you have done int x = (argv[1][j])-48; I don't understand why you are subtracting either 0 or the ascii value of 0 here. Instead of those 3 lines of code, you could consolidate them into one line and eliminate ...


1

Your segmentation fault is due to the following line: if(isalpha(argv[1]))//checks if key is letters isalpha() tests to see if a single character is an alpha. It does not operate on whole strings. When you try to stuff a string into it, it blows up. You need to run a loop across the string, checking each element in argv, i.e., argv[1][i]. As a side note, ...


Only top voted, non community-wiki answers of a minimum length are eligible