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 }

     

  • 相关阅读:
    图像检索(image retrieval)- 11
    图像检索(image retrieval)- 10相关
    Mock.js简易教程,脱离后端独立开发,实现增删改查功能
    Azure Monitor (3) 对虚拟机磁盘设置自定义监控
    Azure Monitor (1) 概述
    Azure SQL Managed Instance (2) 备份SQL MI
    Azure Virtual Network (17) Private Link演示
    Azure Virtual Network (16) Private Link
    Azure Virtual Network (15) Service Endpoint演示
    Azure Virtual Network (14) Service Endpoint服务终结点
  • 原文地址:https://www.cnblogs.com/xiawen/p/3045210.html
Copyright © 2011-2022 走看看