C 語言程序開發範例寶典29

#include <stdio.h>
#include <string.h>
#define N 100
void convert(char s[N]);

 void main()
{
	int i;
	char str[N];
	printf("Enter the string :\n");
	gets(str);
	printf("Origin str :\n %s", str);
	convert(str);
	while (1);

}

void convert(char s[N])
{
	int i, j;
	char temp;
	for (i = 0; i < strlen(s) / 2; i++)
	{
		j = strlen(s) - 1;
		temp = s[i];
		s[i] = s[j-i];
		s[j - i] = temp;

	}
	printf("Now string:\n %s", s);


}

範例寶典中有一段求字符串反轉的程序:gets函數需要獲取字符串輸入,交換通過s[i]=s[j-i] 實現;不論奇偶,想起之前實現一個數組的翻轉,還TM的分情況討論真是浪費!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章