- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
想要对从 argv 获取的两个字符串进行异或。我查了这个问题How to xor two string in C?但它无法为我解决这个问题。
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[]) {
char output[]="";
int i;
for (i=0; i<strlen(argv[1]); i++){
char temp = argv[1][i]^argv[2][i];
output[i]= temp;
}
output[i] = '\0';
printf("XOR: %s\n",output);
return 0;
}
当我使用lldb调试我的输出(“(lldb)打印输出”)时,它是/a/x16/t/x13 但不能用 printf() 打印。我知道它不再是字符串了。你能帮我如何使它能够被 printf:ed 吗?终端中打印的文本是“XOR:”
最佳答案
您的代码中存在一些内存错误。也许以下内容会更好:
#include <stdio.h>
#include <string.h>
#define min(i, j) ((i) < (j) ? (i) : (j))
int main(int argc, char const *argv[])
{
char *output;
int i;
/* Allocate a buffer large enough to hold the smallest of the two strings
* passed in, plus one byte for the trailing NUL required at the end of
* all strings.
*/
output = malloc(min(strlen(argv[1]), strlen(argv[2])) + 1);
/* Iterate through the strings, XORing bytes from each string together
* until the smallest string has been consumed. We can't go beyond the
* length of the smallest string without potentially causing a memory
* access error.
*/
for(i = 0; i < min(strlen(argv[1]), strlen(argv[2])) ; i++)
output[i] = argv[1][i] ^ argv[2][i];
/* Add a NUL character on the end of the generated string. This could
* equally well be written as
*
* output[min(strlen(argv[1]), strlen(argv[2]))] = 0;
*
* to demonstrate the intent of the code.
*/
output[i] = '\0';
/* Print the XORed string. Note that if characters in argv[1]
* and argv[2] with matching indexes are the same the resultant byte
* in the XORed result will be zero, which will terminate the string.
*/
printf("XOR: %s\n", output);
return 0;
}
就 printf
而言,请记住 x ^ x
= 0 并且 \0
是 C 中的字符串终止符。
祝你好运。
关于c - 在 C 中对 argv 中的两个字符串进行异或,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46982214/
目前我正在学习 C 并试图理解这些说明。它们真的不同吗? ++*argv *argv++ *(++argv) *(argv++) 谢谢! 最佳答案 后缀递增运算符的优先级高于指针取消引用运算符,而不是
我正在与在 Hostgator 共享主机上运行的 PHP CLI 的一个(也许)简单问题作斗争。一个简单的代码: 在我的本地机器上运行时以及其他几个 php 实例: php test.php arg
谁能解释一下原因 int main(int argc, const char * argv[]) { while (* argv) puts(* argv++); return 0 ;
从 K&R 的 C 书的第 5.10 章开始,argv 的概念被引入以允许命令行参数。 argv 是指向字符指针数组的指针。据此,以下代码如何检查提供的参数是否以连字符开头? (*++argv)[0]
我不明白 sys.argv 和 argv 之间的区别,网上没有任何东西给我我理解的概念。如果两者相同!我们什么时候使用 sys.argv 什么时候使用 argv ? 如果不是,sys.argv 是什么
我正在编写一个接受参数的 C 程序,char *argv[] 来决定运行哪个 exec()。我有 execlp(argv[1], argv[1], NULL) 可以正常工作。出于某种原因,execvp
char** argv 和 char* argv[] 有什么区别?在 int main(int argc, char** argv) 和 int main(int argc, char* argv[]
我目前有一个 python 脚本,可以在大型网络中完成一项非常有用的任务。我所做的是使用 lsof -iTCP -F 和其他一些选项来转储所有监听的 TCP 套接字。我能够得到 argv[0],这不是
为什么 argvs 的行为很奇怪?示例: 这将按预期工作,打印第一个参数的第一个字符。 printf("%c", *argv[1]); 但是,这将打印 ascii 表中的字符(又名“更大”,表示为数字
我做了一个小的重复异或加密器。您可以使用您选择的键对您选择的字符串进行异或运算,它可以选择将其输出到文件中。 无论何时使用单个单词(无空格)字符串和键运行程序,都没有问题。但是,只要字符串或键中有空格
我刚开始学习 C,想知道我应该在我的主要方法中使用其中的哪一个。有什么不同吗?哪个更常见? 最佳答案 由于您刚开始学习 C,我建议您首先真正尝试了解数组和指针之间的区别,而不是共同的东西。 在参数和数
所以我在编译器转换的函数中传递数组时在幕后阅读过 int myArray(int arr[]) 进入 int myArray(int *arr) 例如,大多数时候数组也会衰减为指针 arr[0] 与相
我正在编写一个小的 shell 程序,它接受一个命令并执行它。如果用户输入无效命令,则 if 语句返回 -1。如果命令正确,它就会执行命令,但是一旦它执行了命令,程序就会结束。我做错了什么是不执行它之
是否可以传入自定义参数列表? 与现实生活中的用例相比,更多是出于好奇,但例如,我可能希望在让 argparse 完成其工作之前将所有参数转换为小写。 最佳答案 Yes .只需传入要parse_args
我知道这个问题很基础,我也是新手,所以请帮我解决这个问题: 我有这个代码: int wmain(int argc, wchar_t *argv[]) { if (*argv[1] == L'-
这个问题在这里已经有了答案: Understanding slicing (38 个答案) 关闭 3 个月前。 我写了这段代码: #!/usr/bin/env python import sys i
我的程序获取主要参数,我的任务是检查用户是否在执行文件和参数之间输入了空格。 这是一个没有在 argv[0] 为 HW01 和 argv[1] 之间输入空格的人的例子/n: HW01/n 但是如果有人
我的 c 技能很生疏,所以如果这是一个愚蠢的问题,我深表歉意,但我什至没想过要寻找这个简单问题的答案。此代码编译时没有任何警告: #include int run_script( int argc,
这个问题在这里已经有了答案: Should I use char** argv or char* argv[]? (10 个答案) What does the 'array name' mean i
我有以下代码片段: int main(int argc, char *argv[]) { char line[MAXLINE]; long lineno = 0; i
我是一名优秀的程序员,十分优秀!