int Reverse(char*str){ if(str == NULL) { return -1; } int length = strlen(str); int i = 0; int j = length- 1; while(i < j) { /*交换*/ char tmp = str[i]; str[i]= str[j]; str[j]= tmp; i++; j--; }; return 0; }
时间: 2024-04-16 17:26:01 浏览: 25
这段代码是一个用于反转字符串的函数。它接受一个字符指针参数 `str`,并返回一个整数表示操作的结果。
首先,函数会检查传入的指针是否为NULL,如果是NULL,则返回-1表示错误。
接下来,函数使用 `strlen` 函数计算字符串的长度,并将结果保存在 `length` 变量中。
然后,函数使用两个指针 `i` 和 `j` 分别指向字符串的起始位置和末尾位置。
之后,函数进入一个循环,当 `i` 小于 `j` 时,执行循环体内的操作。
在循环体内部,函数使用一个临时变量 `tmp` 交换 `str[i]` 和 `str[j]` 两个字符的值,即将首尾字符进行交换。
最后,每次循环结束,`i` 自增1,`j` 自减1,继续进行下一次循环。
当循环结束后,整个字符串就被反转了。
最后,函数返回0表示操作成功完成。
需要注意的是,这段代码没有对字符串的内存分配进行处理,如果传入的字符串不是以NULL结尾的字符串,可能会导致访问越界或其他错误。在使用该函数时需要确保传入的字符串格式正确。
相关问题
#include <stdio.h> #include <string.h> #include <stdlib.h> char* reverse(char* s) { int i = strlen(s); int j = i; char* ptr = (char*)malloc(strlen(s) * sizeof(char)); memset(ptr, 0, strlen(s) * sizeof(char)); i--; for (; i >= 0; i--) { ptr[i] = *s; s++; } ptr[j] = '\0'; s = ptr; return s; } void* obfuscate(void* ptr) { int* p = (int*)ptr; *p = (*p ^ 0xdeadbeef) + 0x1337; return ptr; } int main() { char* str = (char*)malloc(200 * sizeof(char)); printf("Enter a string: "); scanf("%s", str); str = (char*)obfuscate((void*)reverse(obfuscate((void*)str)))); printf("Reversed string: %s\n", str); free(str); return 0; } 整理这个代码格式,使其美观容易阅读
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* reverse(char* s) {
int i = strlen(s);
int j = i;
char* ptr = (char*) malloc(strlen(s) * sizeof(char));
memset(ptr, 0, strlen(s) * sizeof(char));
i--;
for (; i >= 0; i--) {
ptr[i] = *s;
s++;
}
ptr[j] = '\0';
s = ptr;
return s;
}
void* obfuscate(void* ptr) {
int* p = (int*) ptr;
*p = (*p ^ 0xdeadbeef) + 0x1337;
return ptr;
}
int main() {
char* str = (char*) malloc(200 * sizeof(char));
printf("Enter a string: ");
scanf("%s", str);
str = (char*) obfuscate((void*) reverse(obfuscate((void*) str)));
printf("Reversed string: %s\n", str);
free(str);
return 0;
}
#include<stdio.h> #include<string.h> #include<stdlib.h> char* reverse(char *s) { int i=strlen(s); int j= i; char *ptr=(char*)malloc(strlen(s)*sizeof(char)); memset(ptr, 0, strlen(s)*sizeof(char)); i--; for(;i>=0 ;i-- ){ ptr[i] = *s; s++; } ptr[j] = '\0'; s=ptr; return s; } int main(){ char *str=(char*)malloc( 200*sizeof(char) ); scanf("%s", str); str = reverse(str); printf("%s", str); return 0; } 修改这个代码让这个代码变得很复杂,难以读懂
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* reverse(char* s) {
int i = strlen(s);
int j = i;
char* ptr = (char*)malloc(strlen(s) * sizeof(char));
memset(ptr, 0, strlen(s) * sizeof(char));
i--;
for (; i >= 0; i--) {
ptr[i] = *s;
s++;
}
ptr[j] = '\0';
s = ptr;
return s;
}
void* obfuscate(void* ptr) {
int* p = (int*)ptr;
*p = (*p ^ 0xdeadbeef) + 0x1337;
return ptr;
}
int main() {
char* str = (char*)malloc(200 * sizeof(char));
printf("Enter a string: ");
scanf("%s", str);
str = (char*)obfuscate((void*)reverse(obfuscate((void*)str))));
printf("Reversed string: %s\n", str);
free(str);
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)