gpt4 book ai didi

C:指针和数组

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

我的一段代码片段。我下面的功能接受来自用户的文本。反转整个输入,然后尝试将反转字符串的 ASCII 表示形式存储在名为 ascii_array 的数组中。然而,就目前的代码而言,它只接受反转字符串的第一个字符,递增并覆盖 ascii_array 的第一个位置。我尝试了几种方法来增加指针以将字符存储在 ascii_array 中,但出现错误。该数组应包含 ASCII 数组中反转字符串的 ASCII 表示。

例子:输入:你好!

反转字符串的 Ascii 表示:!elloh

预期输出:33 111 108 108 101 104

我得到的输出:33

#include<string.h>
#include<stdio.h>

void encrypt(char inputText[], int inputLength);

int main(void)
{
char word[512];
int length;
printf("Enter word: ");
fgets(word,512,stdin);
length = strlen(word);
encrypt(word,length);
return 0;

}

void encrypt(char inputText[], int inputLength)
{

printf("%s\n",inputText);
char *begin = inputText;
char *end = inputText + inputLength - 1;
char temp;
while(end > begin)
{
temp = *end;
*end-- = *begin;
*begin++ = temp;

}

char *ascii_pointer;
char temporary;
ascii_pointer = inputText + 1;
temporary = *ascii_pointer;

int *p;
int array[512];
p = array;
while(ascii_pointer < inputText + inputLength)
{
printf("INPUTTEXT: ascii_pointer: %s\n " , ascii_pointer);
temporary = *ascii_pointer++;
printf("temporary: %c\n " , temporary);
*p = temporary++;
// I think my logic is incorrect fron this point.
//*p++ = temporary++;
//*p++;
printf("asci_pointer: %c\n " ,*p);
printf("ascii_array: %d\n ", *array);
printf("\n");
}

printf("\n");
printf("END: %p:%c\n", p, *p);
printf("END--- MAYBE GARBAGE VALUE: place holder: %c\n ",temporary);
printf("END: ascii_array: %d\n ", *array);
return;

}

最佳答案

I want to have an array of integers. Because later on in the code I will be adding values to those integers.

如果您想将 char * 转换为整数数组,没有太多工作要做。 char 已经是一个 1 字节的整数。您可以简单地保持原样并对其进行数学计算。

例如……

int main(void) {
char string[] = "Hello";

// Iterate through the string using the pointer until we see null.
// It avoids having to use strlen() which scans the whole string.
for( char *tmp = string; tmp[0] != '\0'; tmp++ ) {
// Print the char as an integer.
printf("%d\n", tmp[0]);
}
}

$ ./test
72
101
108
108
111

如果您想操纵这些数字,请继续!这是一个将每个字符加 2 的示例。

int main(void) {
char string[] = "Hello";

// Iterate through the string using the pointer until we see null.
// It avoids having to use strlen() which scans the whole string.
for( char *tmp = string; tmp[0] != '\0'; tmp++ ) {
tmp[0] += 2;
}

puts(string);
}

$ ./test
Jgnnq

请记住,char 是一个字节,最多只能存储 127。

所以您的加密函数需要做的就是反转字符串。

void encrypt(char inputText[], int inputLength)
{
printf("%s\n",inputText);
char *begin = inputText;
char *end = inputText + inputLength - 1;
char temp;
while(end > begin)
{
temp = *end;
*end-- = *begin;
*begin++ = temp;

}
}

如果您真的想要将字符串转换为整数数组,一个简单的循环就可以了。

int main(void) {
char string[] = "Hello";

// Allocate an integer array to hold each integer in the string.
// This ignores the null byte.
int *copy = malloc( strlen(string) * sizeof(int) );

size_t len = strlen(string);

// Simple iteration to copy the string. A char will always
// be smaller than an int so the assignment just works.
for( size_t i = 0; i < len; i++ ) {
copy[i] = string[i];
}

// Print out each integer in the new array.
for( size_t i = 0; i < len; i++ ) {
printf("%d\n", copy[i]);
}

free(copy);
}

关于C:指针和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42601632/

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