/*
//2.10分
2.字符串反转
输入 abcdef --->输出 fedcba
*/
#import <Foundation/Foundation.h>
#define LEN 100
int main(int argc, const char * argv[]) {
//定义数组保存用户输入的字符串
char str[LEN];
NSLog(@"请您输入字符串");
rewind(stdin);
//接收用户输入的字符串
fgets(str,LEN, stdin);
size_t len = strlen(str);
if (str[len-1] == '\n') {
str[len-1] = '\0';
}
//逆序数组数组
int i = 0;
size_t j = len - 1;
while (i<j) {
char ch = str[i];
str[i] = str[j];
str[j] = ch ;
i++;
j--;
}
//打印逆序后的数组
for (int i = 0; i<len; i++) {
printf("%c",str[i]);
}
printf("\n");
return 0;
}
|
|