zoukankan      html  css  js  c++  java
  • 杭电1062-字符串翻转

    这是天津大学2015考研的编程题

    Problem Description
    Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
     
    Input
    The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
    Each test case contains a single line with several words. There will be at most 1000 characters in a line.
     
    Output
    For each test case, you should output the text which is processed.
     
    Sample Input
    3 olleh !dlrow
    m'I morf .udh
    I ekil .mca
     
    Sample Output
    hello world!
    I'm from hdu.
    I like acm.
    Hint
    Remember to use getchar() to read ' ' after the interger T, then you may use gets() to read a line and process it.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main()
    {
        char c,buffer[1001];
        char* text_end;//每段字符串的结束地址
        char* text_begin;//每段字符串的开始地址
        int i=0,j=0;
        int cnt;
        char  temp;
     
        scanf("%d",&cnt);
        while(getchar()!=' ');//输入case数目的时候错了,你只读最后一个字符。。13你会读成3。。
        while(i<cnt)
        {
            gets(buffer);
            strcat(buffer," ");//为了方便输出,将输入的字符添加空格和字符串结束符
            text_begin=&buffer[0];//初始化第一段字符串的起始地址
            for(j=0; j<strlen(buffer); j++)
            {
                if(buffer[j]==' ')//遇到空格时,将字符串翻转
                {
                    text_end=&buffer[j-1];//字符串的结束地址
                    while(text_begin<text_end)//字符串翻转
                    {
                        temp=*text_begin;
                        *text_begin=*text_end;
                        *text_end=temp;
                        text_begin++;
                        if(text_begin==text_end) break;
                        text_end--;
                    }
                    text_begin=&buffer[j+1];//每段字符串结束重置开始地址
                }
            }
            buffer[strlen(buffer)-1]='';//删除结尾字符串的空格字符
            printf("%s ",buffer);
            i++;
        }
        return 0;
    }

    另外看到一个解法:(用栈实现)

    #include <iostream>
    #include <cstring>
    #include <stack>
    using namespace std;
    int main()
    {
        int T;
        cin >> T;
        while(T --)
        {
            stack<char> ch_stk;        //思路是一个个单词扔栈里面再弹出就反转了
            string ori_msg;
            string ch_msg;
            getline(cin, ori_msg);
            ch_msg.resize(ori_msg.length());
            int j = 0;
            for(int i=0; i<ori_msg.length(); i++)
            {
                if(ori_msg[i] != ' ')              //
                    ch_stk.push(ori_msg[i]);
                else
                {
                    while(!ch_stk.empty())
                    {
                        ch_msg[j++] = ch_stk.top();
                        ch_stk.pop();
                    }
                   // if(i != ori_msg.length()-1)          //句子原来的空格扔回去
                    ch_msg[j++] = ' ';
                }
            }
            while(!ch_stk.empty())                  //处理最后一个单词,因为这里已经跳出循环了
            {
                ch_msg[j++] = ch_stk.top();
                ch_stk.pop();
            }
            cout << ch_msg << endl;
        }
        return 0;
    }

    一个精简版本:

    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
        char str[1000]={''};
        int n;
        cin>>n;
        getchar();
        while(n--)
        {
            cin.getline(str,1000);
            int slen=strlen(str);
            int flag=0;
            int i,j;
            for(i=0;i<strlen(str);i++)
            {
                if(str[i]==' ')
                {
                    for(j=i-1;j>=flag;j--)
                    {
                        cout<<str[j];
                    }
                    cout<<" ";
                    flag=i+1;
                }
            }
             for(j=i-1;j>=flag;j--)
             {
                cout<<str[j];
             }
             cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    单调栈
    ROS 运行Python程序报错:ERROR: cannot launch node of type [teleop/teleop_key]: can't locate node [teleop_key] in package [teleop]
    c++ dynamic_cast 笔记
    C++ STL map 随手笔记
    TDD 中关于mock一些理解
    C++ struct 初始化的问题
    C++ count_if/erase/remove_if 用法详解
    C++ 编译报错discards qualifiers [-fpermissive]
    笔记本外接显示器没有声音
    virtual box ubuntu 主机和虚拟机实现互相复制粘贴
  • 原文地址:https://www.cnblogs.com/wft1990/p/4323272.html
Copyright © 2011-2022 走看看