gpt4 book ai didi

c - strrev 函数在字符串数组的第一个元素中插入一个换行符

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

所以基本上:

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

int main(void){
//test strrev
char s[50];
char s2[50];
char *ps;
int i=0;
printf("String to reverse: ");
fgets(s,50,stdin);
ps=strrev(s);
strcpy(s2,ps); //copy contents to a string array
//i did the copy because using printf("%s", ps); did the same thing

printf("Reversed string: %s\n", s2); //PECULIAR, s2 enters line feed char in s2[0]

//test loop to determine the inserted character
while(1){
if(s2[i]==10) {printf("is 10,%d", i); break;}; //the proof of LF
if(s2[i]==12) {printf("is 12"); break;};
if(s2[i]==13) {printf("is 13"); break;};
if(s2[i]==15) {printf("is 15"); break;};
i++;
}
for(i=0;i<50;i++){ //determine where the characters are positioned
printf("%c: %d\n", s2[i], s2[i]);
if(s2[i]=='\0') break;
}

system("PAUSE");
return 0;
}

通过运行此程序并输入字符串....假设“darts”将反转数组中包含元素 s2[0]='\012 的字符串'=10(十进制), ...strad..., s2[7]='\0'.strrev 这样的行为正常吗?

最佳答案

fgets 将换行符存储在字符串中。所以当你 strrev 时,\n(换行)将是第一个元素。

The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a is read and transferred to s.

编辑

刚刚在 Visual Studio 上测试过:

char a[] = "abcd\n";
strrev(a);

printf("%d\n", a[0]); /* 10 */

关于c - strrev 函数在字符串数组的第一个元素中插入一个换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6808087/

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