zoukankan      html  css  js  c++  java
  • 【原创】编程题练习:反转字符串中的单词

    比较老的一道题了,练练手,面向过程的coding

     

     

     1 #include<iostream>
     2 
     3 using namespace std;
     4 int main()
     5 {
     6     string str = "i am a student.";
     7     int i= 0,j = str.length()-1;
     8     char temp;
     9     while(i < j)
    10             {
    11                 temp = str[i];
    12                 str[i] = str[j];
    13                 str[j] = temp;
    14                 i++;
    15                 j--;
    16             }
    17     i = 0;j=0;
    18     int temp1,temp2;
    19     while(j<str.length())
    20     {
    21         if(str[j] != ' '&& j != str.length()-1)
    22         {
    23             j++;
    24             continue;
    25         }
    26         else if(str[j] == ' ')
    27         {
    28             temp1 = i;
    29             j -= 1;
    30             temp2 = j;
    31             while(i<j)
    32             {
    33                 temp = str[i];
    34                 str[i] = str[j];
    35                 str[j] = temp;
    36                 i++;
    37                 j--;
    38             }
    39             i = temp2+2;
    40             j = temp2+2;
    41         }
    42         if(j == str.length()-1)
    43         {
    44             while(i<j)
    45             {
    46                 temp = str[i];
    47                 str[i] = str[j];
    48                 str[j] = temp;
    49                 i++;
    50                 j--;
    51             }
    52             j = str.length();
    53         }
    54 
    55     }
    56 
    57     cout << str << endl;
    58     
    59     system("pause");
    60     
    61     return 0;
    62 
    63 }

     

  • 相关阅读:
    编写高质量Python程序(四)库
    编写高质量Python程序(三)基础语法
    编写高质量Python程序(二)编程惯用法
    编写高质量Python程序(一)基本准则
    IM聊天教程:发送图片/视频/语音/表情
    微信小程序使用GoEasy实现websocket实时通讯
    Websocket直播间聊天室教程
    不将就!GoEasy消息推送助力一加手机8系线上发布会
    手把手教你用GoEasy实现Websocket IM聊天
    Uniapp使用GoEasy实现websocket实时通讯
  • 原文地址:https://www.cnblogs.com/xiawen/p/3045210.html
Copyright © 2011-2022 走看看