方法一:在leetcode编译通过:
1 char *reverse(char *s) //字符串翻转函数 2 { 3 int iLength = strlen(s); 4 int i = 0; 5 for(;i<iLength/2;i++) 6 { 7 *(s+i) = *(s+i) ^ *(s+iLength-i-1); //进行异或操作,比较是否相同。 8 *(s+iLength-i-1) = (*(s+iLength-i-1)) ^ (*(s+i)); 9 *(s+i) = *(s+i) ^ *(s+iLength-i-1); 10 } 11 return s; 12 } 13 char * thousandSeparator(int n){ 14 char *s = (char *)malloc(30); 15 memset(s, 0, 30); 16 char *s1=(char *)malloc(30); 17 memset(s, 0, 30); //初始化字符串(必须有) 18 sprintf(s1,"%d",n); //整数转字符 19 int count=0; //记录原字符串个数 20 int i; 21 int index=0; //记录加入标点号后的数组个数 22 for(i=strlen(s1)-1;i>=0;--i) 23 { 24 s[index++]=s1[i]; 25 ++count; 26 if(count%3==0&&i) 27 { 28 s[index++]='.'; 29 } 30 } 31 reverse(s); 32 return s; 33 }
方法二:在dev-c++程序中运算通过。
#include<stdio.h> #include<string.h> /* char *reverse(char *s) { int iLength = strlen(s); int i; for(i=0;i<iLength/2;i++) { *(s+i) = *(s+i) ^ *(s+iLength-i-1); *(s+iLength-i-1) = (*(s+iLength-i-1)) ^ (*(s+i)); *(s+i) = *(s+i) ^ *(s+iLength-i-1); } return s; } */ int main() { int n; char s[32],s1[32]; scanf("%d",&n); sprintf(s1,"%d",n); int count=0; int i; int index=0; for(i=strlen(s1)-1;i>=0;--i) { s[index++]=s1[i]; ++count; if(count%3==0&&i) { s[index++]='.'; } } strrev(s); puts(s); return 0; }