/*
'\'在s[]中最后出现的位置是:11
t[] = 123
Press any key to continue
*/
#include <stdio.h>
#include <string.h>
// 返回ch在s[]最后出现的位置。返回0表示在s[]中没有发现字符ch
int LastPos(char s[], char ch) {
int i,pos = 0; //
for(i = 0; s[i]; ++i)
if(s[i] == ch) pos = i + 1;
return pos;
}
// 复制s[]中右边的n个字符到t[]中
char *RightStr(char s[], char t[], int n) {
int i,j,len = strlen(s);
t[0] = '\0';
if(n >= len) strcpy(t,s);
else if(n > 0 && n < len) {
i = len - n;
j = 0;
while(t[j++] = s[i++])
;
}
return t;
}
int main() {
char s[] = "C:\\WINDOWS\\123";
char t[20];
printf("'\\'在s[]中最后出现的位置是:%d\n",LastPos(s,'\\'));
printf("t[] = %s\n",RightStr(s,t,3));
return 0;
}