C语言反转一行字符串

#include <stdio.h>
#include <string.h>

int string_length(char s[]) {
   int i = 0;

   while(s[i]!='/0')
      i++;

   return i;    
}

void string_reverse(char st[]) {
   int i,j,len;
   char ch;

   j = len = string_length(st) - 1;
   i = 0;

   while(i < j) {
      ch = st[j];
      st[j] = st[i];
      st[i] = ch;
      i++;
      j--;
   }
}

int main (void) {
   char line[] = "Taj Mahal is one of the seven wonders of the world";
   char reverse[100] = "", temp[50];
   int i, j, n;

   n = string_length(line);

   for(i = n-1; i >= 0; --i) {

      for(j = 0; i >= 0 && line[i] != ' '; --i,++j)
         temp[j] = line[i];

      temp[j] = '/0';

      string_reverse(temp);

      strcat(reverse,temp);
      strcat(reverse," ");
   }

   printf("Original - %s/n", line);
   printf("Reversed - %s/n",reverse);

   return 0;
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/265347.html

(0)
上一篇 2022年6月7日
下一篇 2022年6月7日

相关推荐

发表回复

登录后才能评论