zoukankan      html  css  js  c++  java
  • 字符串反转&说反话

    题目描述

    写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)

    输入描述:

    输入N个字符

    输出描述:

    输出该字符串反转后的字符串

    示例1

    输入

    abcd
    

    输出

    dcba

    解题思路就不写了,太简单了,直接记录一下代码,以前写的简单题都没做记录导致想找的时候都不容易找到,所以还是随手记一下吧。
    #include<iostream>
    #include<vector>
    #include<string>
    using namespace std;
    
    char c[1001];
    int main() {
    
        cin >> c;
        vector<char> v;
        int i = 0;
        //将输入的字符串存入容器中
        while(c[i]!=''){
            v.push_back(c[i]);
            i++;
        }
        //倒序遍历容器输出
        vector<char>::reverse_iterator vit;
    
        for (vit = v.rbegin(); vit != v.rend(); vit++) {
            cout << *vit;
        }
        cout << endl;
        system("pause");
        return 0;
    }

    给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

    输入格式:

    测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用 1 个空格分开,输入保证句子末尾没有多余的空格。

    输出格式:

    每个测试用例的输出占一行,输出倒序后的句子。

    输入样例:

    Hello World Here I Come
    
     

    输出样例:

    Come I Here World Hello


    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    int main() {
        int num = 0;//单词个数
        
        while (scanf_s("%s", word[num]) != EOF) {
            num++;
        }
        for (int i = num - 1; i >= 0; i--) {//倒着输出单词
            if (i != num - 1) cout << " ";
            printf("%s", word[i]);
        }
             cout << endl;
        system("pause");
        return 0;
    }
    
    由于在黑框中输入时,系统并不知道什么时候到达了文件末尾,所以需要按<ctrl+z>在加enter告诉系统到达了文件末尾,这样才能结束while循环
    
    
    #include<iostream>
    #include<cstring>
    
    using namespace std;
    
    int main() {
        char word[90][90] = {' '};//存放单词
        char str[90];
        gets_s(str);//输入str
        int len = strlen(str), row = 0, col = 0;
        for (int i = 0; i < len; i++) {
            if (str[i] != ' ') {
                //如果当前字符不是空格,存入字符串数组中
                word[row][col++] = str[i];
            }
            else {
                //是空格,说明上一个单词已经结束
                word[row][col] = '';//单词末尾结束符
                row++;
                col = 0;//下一个单词从第0列开始
    
            }
        }
        //倒着输出单词
        for (int i = row; i >= 0; i--) {
            if (i != row) cout << " ";
            cout << word[i];
        }
        cout << endl;
        system("pause");
        return 0;
    }
    唯有热爱方能抵御岁月漫长。
  • 相关阅读:
    Elasticsearch6.x和7.x版本常用插件汇总
    阿里巴巴JAVA开发规范学习笔记
    jQuery学习和知识点总结归纳
    MySQL常用维护命令和操作
    MySQL知识点系统总结
    HTML基础知识自学教程
    最值得拥有的免费Bootstrap后台管理模板
    强烈推荐优秀的Vue UI组件库
    再次学习Git版本控制工具
    Linux下Apache虚拟主机配置
  • 原文地址:https://www.cnblogs.com/syq816/p/12489767.html
Copyright © 2011-2022 走看看