如何将一组更改放入一个数组,然后在 C 中将它们作为一个打印出来
How to put a group of changes to an array and then print all of them as one in C
我目前正在解决一个编码问题。我必须加密一个代码并将单词更改为其他编码的单词。我知道我可以检查并打印每个字符。但是,我想尝试将每个字符的检查结果保存到一个数组中,然后我想打印它。
下面是完整的代码...如果有人能帮我回答我的问题,或者甚至可能的话,我将不胜感激。
//one command line argument with the type int
int main(int argc, string argv[])
{
//setting condition that: K = +; if more or less than one, immediate error message, return 1
//if not decimal return = usage ./caesar. key, return value 1 to main
if (argc != 2)
{
printf("Error 404 : \n");
return 1;
}
//main program
else if ( argc == 2 && isdigit(*argv[1]))
{
int k = atoi(argv[1]);
string pltext = get_string("plaintext: "); //getting input for the plain text
char cptext[]
for (int i = 0, n = strlen(pltext) ; i < n; i++) //turning pltext to integer
{
if (pltext[i] >= 'a' && pltext[i] <= 'z')
{
cptext[i] = ((pltext[i] - 'a' + k)%26)+'a'; //shifting the integer with k (lowercase)
}
else if (pltext[i] >= 'A' && pltext[i] <= 'Z')
{
cptext[i] = ((pltext[i] - 'A' + k)%26)+'A'; //shifting the integer with k (uppercase)
}
else
{
cptext[i] = pltext[i]; //other symbol stays
}
}
//print out result
string cptext = ("test");
printf("ciphertext: %s\n", cptext[]);
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
你应该根据要存储的元素个数来分配一个数组。
char cptext[]
应该是
char cptext[strlen(pltext) + 1]; // +1 for terminating null-character
(C99引入的变长数组)
和
string cptext = ("test");
printf("ciphertext: %s\n", cptext[]);
应该是
cptext[strlen(pltext)] = '[=13=]'; // terminate the string
printf("ciphertext: %s\n", cptext); // remove extra []
我目前正在解决一个编码问题。我必须加密一个代码并将单词更改为其他编码的单词。我知道我可以检查并打印每个字符。但是,我想尝试将每个字符的检查结果保存到一个数组中,然后我想打印它。
下面是完整的代码...如果有人能帮我回答我的问题,或者甚至可能的话,我将不胜感激。
//one command line argument with the type int
int main(int argc, string argv[])
{
//setting condition that: K = +; if more or less than one, immediate error message, return 1
//if not decimal return = usage ./caesar. key, return value 1 to main
if (argc != 2)
{
printf("Error 404 : \n");
return 1;
}
//main program
else if ( argc == 2 && isdigit(*argv[1]))
{
int k = atoi(argv[1]);
string pltext = get_string("plaintext: "); //getting input for the plain text
char cptext[]
for (int i = 0, n = strlen(pltext) ; i < n; i++) //turning pltext to integer
{
if (pltext[i] >= 'a' && pltext[i] <= 'z')
{
cptext[i] = ((pltext[i] - 'a' + k)%26)+'a'; //shifting the integer with k (lowercase)
}
else if (pltext[i] >= 'A' && pltext[i] <= 'Z')
{
cptext[i] = ((pltext[i] - 'A' + k)%26)+'A'; //shifting the integer with k (uppercase)
}
else
{
cptext[i] = pltext[i]; //other symbol stays
}
}
//print out result
string cptext = ("test");
printf("ciphertext: %s\n", cptext[]);
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
}
你应该根据要存储的元素个数来分配一个数组。
char cptext[]
应该是
char cptext[strlen(pltext) + 1]; // +1 for terminating null-character
(C99引入的变长数组)
和
string cptext = ("test");
printf("ciphertext: %s\n", cptext[]);
应该是
cptext[strlen(pltext)] = '[=13=]'; // terminate the string
printf("ciphertext: %s\n", cptext); // remove extra []