zoukankan      html  css  js  c++  java
  • c 转置字符串You are a so cheap man ->man cheap so a are You

    解题思路:

    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) = '';
    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("%[^
    ]", arr);   //遇到回车符结束,这样可以接收空格符
    63     tranStr(arr,brr);
    64     tranStrWord(brr);
    65     puts(brr);
    66     return 0;
    67 }
  • 相关阅读:
    BZOJ1430小猴打架——prufer序列
    [集训队作业2018]蜀道难——TopTree+贪心+树链剖分+链分治+树形DP
    BZOJ5063旅游——非旋转treap
    bzoj 4570 妖怪
    Luogu 1452 Beauty Contest
    bzoj 1337 最小圆覆盖
    bzoj 1007 水平可见直线
    Luogu 4724 三维凸包
    bzoj 4827 礼物
    hdu 4348 To the moon
  • 原文地址:https://www.cnblogs.com/lhy5678888/p/4424030.html
Copyright © 2011-2022 走看看