將字串反向輸出
阿新 • • 發佈:2018-12-13
(1):
#include<stdio.h> #include<stdlib.h> char* Rever_string(char* p) { int n = 0; char* q; char temp; q = p; while (*p != 0) { p++; n++; } if (n > 1) { temp = q[0]; q[0] = q[n - 1]; q[n - 1] = '\0'; Rever_string(q + 1); q[n - 1] = temp; } return q; } int main() { char p[] = "asdfghjkl"; printf("翻轉前:%s\n", p); printf("翻轉後:%s\n", Rever_string(p)); system("pause"); return 0; }
(2):
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void Printf()
{
char a;
scanf("%c", &a);
if (a != '#')
{
Printf();
}
if (a != '#')
{
printf("%c", a);
}
}
int main()
{
printf("請輸入一個人以#結尾的字串:\n");
Printf();
system("pause");
return 0;
}