基本上是这样的:
#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这样做是否正常?
答案 0 :(得分:4)
fgets
将换行符存储在字符串中。因此,当strrev
时,\n
(换行)将成为第一个元素。
fgets()
函数应从流中读取字节到数组中 s指向,直到读取n-1个字节,或读取 并转移到s 。
修改强>
刚在Visual Studio上测试过:
char a[] = "abcd\n";
strrev(a);
printf("%d\n", a[0]); /* 10 */
答案 1 :(得分:2)
strrev
反转给定字符串中字符的顺序。结尾的空字符\0
仍然存在。
fgets
在这种情况下存储换行符。
答案 2 :(得分:1)
fgets
将换行符存储在char数组中作为最后一个字符。 strrev将保留此字符以及以null结尾的字符(例如\ 010)。 ASCII 10是换行符。该字符串以null结尾(\0
),因此strrev
只是采用以null结尾的char
数组并将其反转。如果您不想要以null终止的字符,则将其删除。 strrev
反转给定字符串中字符的顺序。结束的空字符(\ 0)保持不变。