用C語言編寫程式:接收使用者輸入的字串,並以相反的順序輸出該字串

  • 作者:由 匿名使用者 發表于 詩詞
  • 2022-08-03

用C語言編寫程式:接收使用者輸入的字串,並以相反的順序輸出該字串匿名使用者2022.05.30 回答

#include

#include

int main()

{

int i,len; //接受字串長度

char str[10000]; //定義字元型陣列 開到10000應該夠長了

scanf(“%s”,str); //向str陣列中輸入字串

// gets(str); //這裡是另一種方法 用這種可以讀入空格

len=strlen(str); //使用strlen函式 得到字串長度

for(i=len-1;i>=0;i——)

printf(“%c”,str[i]); //反序輸出 這裡字串長度減1 因為陣列是從0開始的

printf(“\n”); //換行

return 0;

}

用C語言編寫程式:接收使用者輸入的字串,並以相反的順序輸出該字串匿名使用者2018.12.23 回答

#include

#include

int main(void){

char i[64];

int j,k;

printf(“請輸入一個字串:”);

gets(i);

printf(“你輸入的字串是:%s\n”,i);

j=strlen(i);

printf(“字串長度是:%d\n”,j);

printf(“你輸入的逆轉結果:”);

for (k=0;k<=strlen(i);k++){

printf(“%c”,i[——j]);

}

return 0;

}

用C語言編寫程式:接收使用者輸入的字串,並以相反的順序輸出該字串匿名2016.12.05 回答

#include

#include

int main() {

char s[1000], c[1000];

unsigned int n,i=0,len=0;

scanf(“%d”, &n);

while(i++

gets(s);

if(strlen(s) > len){

strcpy(c, s);

len = strlen(s);

}

}

printf(“%s”, c);

return 0;

}

用C語言編寫程式:接收使用者輸入的字串,並以相反的順序輸出該字串匿名使用者2016.04.11 回答

#include

#include

#include

int main()

{

char input[1024];

int len, i;

printf(“Input string。。。\n”);

gets(input);

len = strlen(input);

for (i = len-1; i>=0; i——)

{

printf(“%c”, input[i]);

}

printf(“\n”);

getch();

return 0;

}

用C語言編寫程式:接收使用者輸入的字串,並以相反的順序輸出該字串匿名使用者2010.06.11 回答

#include

#include

void main()

{

char arr[100],temp;

int i,arr_size;

puts(“Input the string: ”);

gets(arr);

arr_size=strlen(arr);

for (i=0;i

{

temp=arr[i];

arr[i]=arr[arr_size-1-i];

arr[arr_size-1-i]=temp;

}

puts(“The sorted string: ”);

puts(arr);

}

用C語言編寫程式:接收使用者輸入的字串,並以相反的順序輸出該字串匿名使用者2010.06.11 回答

#include

#include

#include

typedef struct {

char s[512];

char (*fun1)(char *src);

}func;

char *revers(char *src);

int main(void)

{

func *test=(func *)malloc(sizeof(func));

test->fun1=revers;

strcpy(test->s,“abc”);

test->fun1(test->s);

printf(“%s”,test->s);

}

char *revers(char *src)

{

char *tmp=src; //使用tmp保留src

char *firstpos;

char *lastpos;

firstpos=src;//指向src第一個字元

while(*tmp != ‘\0’) tmp++;

lastpos=tmp-1;//指向src最後一個字元

while(firstpos

{

/*交換,沒有用中間變數*/

*firstpos=(*firstpos)+(*lastpos);

*lastpos=(*firstpos)-(*lastpos);

*firstpos=(*firstpos)-(*lastpos);

firstpos++;

lastpos——;

}

return src;

}

Top