c 转置字符串You are a so cheap man ->man cheap so a are You

本文介绍了一种将字符串转置并对其内部单词进行转置的算法实现,包括字符串转置、判断字母、转置单词及对整个字符串中单词转置的过程,并通过C语言代码实例进行了详细说明。

解题思路:

1、将字符串转置

2、对转置后的字符串中单词转置

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<stdbool.h>
 5 //字符串转置
 6 void tranStr(const char *p,char *s)
 7 {
 8     int len = strlen(p);
 9     int i,j;
10     for(i=len,j=0; i>=0,j<len; i--,j++)
11     {
12         *(s+j) = *(p+i-1);
13     }
14     *(s+j) = '\0';
15 }
16 
17 //判断是否为字母
18 bool isChar(char c)
19 {
20     bool isC = false;
21     if((c >='a' && c<='z')
22        ||(c>='A'&&c<='Z'))
23         isC = true;
24     return isC;
25 }
26 //转置单词
27 void tranWord(char *a,int count)
28 {
29     int i;
30     char temp;
31     for(i=0; i<count/2; i++)
32     {
33         temp = *(a+count-1-i);
34         *(a+count-1-i) = *(a+i);
35         *(a+i) = temp;
36     }
37 }
38 
39 //对转置字符串中的单词转置
40 void tranStrWord(char *a)
41 {
42     int i=0,count=0,len;
43     for(i=0; i<len; i++)
44     {
45         if(isChar(*(a+i)))
46             count++;
47         else
48         {
49             tranWord(a+i-count,count);
50             count = 0;
51         }
52         if(count>0 && i==len-1)
53         {
54             tranWord(a+i-count+1,count);
55         }
56     }
57 }
58 
59 int main(void)
60 {
61     char arr[50],brr[50];
62     scanf("%[^\n]", arr);   //遇到回车符结束,这样可以接收空格符
63     tranStr(arr,brr);
64     tranStrWord(brr);
65     puts(brr);
66     return 0;
67 }

 

转载于:https://www.cnblogs.com/lhy5678888/p/4424030.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值