字符和字符串操作————35.字符串倒置

xiaoxiao2021-02-28  70

要求说明:输入字符串,要求将该字符串倒置输出。例如:输入abcdef,输出fedcba。

//字符串倒置#include <stdio.h>#include <string.h>#define N 100void convert(char s[])              //转换函数{    int i,j;    char temp;    for(i = 0; i < strlen(s)/2;i ++)    //由中间值作对称中心对换两边值    {        j = strlen(s)-1;                //下标从0开始,所以字符串长度减1        temp = s[i];        s[i] = s[j-i];        s[j-i] = temp;    }    printf("now string:\n%s",s);        //输出转换后的字符串}int main(int argc, char **argv){    char str[N];    printf("enter the string:\n");    gets(str);                          //输入字符串    printf("origin str:\n%s\n",str);    //输出原来字符串    convert(str);                       //调用转换函数    return 0;

}

样例输入输出:

enter the string:hello world!origin str:hello world!now string:!dlrow olleh

转载请注明原文地址: https://www.6miu.com/read-2620327.html

最新回复(0)