gpt4 book ai didi

c - 在 C 中的 char 数组的前半部分中查找每隔一个字符的程序

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:22 24 4
gpt4 key购买 nike

这是程序的第一部分,我有几个问题想知道它的各个部分是如何工作的。请记住,这是我编写的第一个 C 程序。 scanf("%d",&numberOfTimes);为什么我需要 & 它有什么作用?字符输入[][200];这基本上是一个字符串数组还是完全不同的东西?

#include <stdio.h>
#include <string.h>
char outputs[100];
char input[][200];
int numberOfTimes;

void io(void){
scanf("%d", &numberOfTimes);
for(int i = 0; i < numberOfTimes; i++){
scanf("%s",input[i]);
}
}

代码的下一部分是我尝试实际解决问题的尝试,但是我怀疑我搞砸了一个函数的使用,但我不知道是哪个函数或者我使用不当的东西来得到这个结果。 (我在底部提供了我的示例 i/o 代码)。

void stringManipulation(char string[200]){
int strLength = strlen(string);
int number = strLength/2;
for(int i = 0; i <= number; i=i+2){
strcat(outputs,&string[i]);
}
}



int main(void) {
io();
for(int i = 0; i < numberOfTimes; i++) {
stringManipulation(input[i]);
printf("%s\n",outputs);
memset(&outputs[0], 0, sizeof(outputs));
}
return 0;
}

我是否正确使用了 memset?我还是不明白 & 的用法。

示例输入:

4

your

progress

is

noticeable

预期输出:

y

po

i

ntc

我得到的输出:

yourur

progressogressress

is

noticeableticeableceable

感谢您的帮助。

最佳答案

变量前的 & 表示您指的是变量的地址,而不是变量值本身。如果您不知道变量的地址是什么:http://www.cquestions.com/2010/02/address-of-variable-in-c.html

char input[][200] 是 char 数组的数组(char 数组被粗俗化为字符串,但它不是 TYPE)。

你的问题是关于你的 strcat 使用,你添加的字符介于 string[i] (包括)和 string[strLength],不仅是 string[i] 字符。

关于c - 在 C 中的 char 数组的前半部分中查找每隔一个字符的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37427030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com