Answer the question
In order to leave comments, you need to log in
How to return to the beginning of the line?
We need to encrypt argv[1] with argv[2]. There are 2 user-supplied arguments, both arguments are strings. Accordingly, the first argument receives a conditional text that will need to be encrypted, for example "ENCODE ME", the second argument is the very key with which the encoding takes place, for example "KEY". So, I would like "KEY" to be applied character by character and not stop working after a single execution. Due to lack of experience, it is not possible to do exactly this, tell me how you can implement it. I tried a lot of options, tried to make a loop so that after the word ends, return to the null character and run it again, but it didn’t work out, because I can’t think of anything to replace in C, indexOf in JS.
for (int i = 0; i < argv[1][i]; i++)
{
if (isalpha(argv[1][i]))
{
if (argv[1][i] >= 'A' && argv[1][i] <= 'Z')
{
int shift = argv[2][i] - 65;
printf("%c + %c '%.3d' == ", argv[1][i], argv[2][i], shift);
char rightNumber = argv[1][i] + shift;
if (rightNumber > 'Z')
rightNumber -= 26;
printf("%c\n", rightNumber);
}
}
}
Answer the question
In order to leave comments, you need to log in
Replace all argv[2][i] with argv[2][i % strlen(argv[2])] or create a separate variable under a new index.
It's standard practice to go around in circles - taking the remainder of the index divided by the range.
PS. The condition in the loop i < argv[1][i] is very dubious.
#include <string.h>
#include <stdio.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int incode_len = strlen(argv[1]);
int key_len = strlen(argv[2]);
for (int i = 0, k = 0; i < incode_len; i++, k++)
{
if( k >= key_len) k = 0;
if (isalpha(argv[1][i]))
{
if (argv[1][i] >= 'A' && argv[1][i] <= 'Z')
{
int shift = argv[2][k] - 65;
printf("%c + %c '%.3d' == ", argv[1][i], argv[2][k], shift);
char rightNumber = argv[1][i] + shift;
if (rightNumber > 'Z') rightNumber -= 26;
printf("%c\n", rightNumber);
}
}
}
}
Cyclic String Code Example
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
const char *s, *start, *end, *cur;
int n, i;
if (argc == 3) {
s = argv[1];
n = atoi(argv[2]);
} else {
s = "12345";
n = 50;
}
start = end = s;
if (*end)
while (*(end + 1))
end++;
cur = start;
for (i = 0; i < n; i++) {
putchar(*cur);
cur = (cur < end) ? cur + 1 : start;
}
putchar('\n');
return 0;
}
[[email protected] c]$ .ansi t.c -o t
[[email protected] c]$ ./t
12345123451234512345123451234512345123451234512345
[[email protected] c]$ ./t abc 80
abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcab
[[email protected] c]$ ./t a 10
aaaaaaaaaa
[[email protected] c]$ ./t ab 10
ababababab
[[email protected] c]$ ./t abc 10
abcabcabca
[[email protected] c]$
#include <string.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned char *text, *key;
unsigned char shift, rightNumber;
for (text = argv[1], key = argv[2]; *text; text++, key++) {
if (0 == *key)
key = argv[2];
if (*text >= 'A' && *text <= 'Z') {
shift = *key - 'A';
rightNumber = *text + shift;
if (rightNumber > 'Z')
rightNumber -= ('Z' - 'A');
printf(""%c + %c '%.3d' == "%c\n", *text, *key, shift, rightNumber);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question