算法练习1:Take every 2nd char from the string. Then the other chars 随笔
来源:互联网 发布:twitter是什么软件 编辑:程序博客网 时间:2024/04/29 07:59
题目和要求:
十分全的string函数:
http://www.jb51.net/article/41725.htm
可能用到的函数:
-------删除函数 :erase();
#include<string>using namespace std;...string s1="asdfgh", s2;s2=s1.erase(2,3); //删除s1中从第2个字符其的3个字符。则s2的内容为"ash"
--------赋值函数:assign();
str2.assign(str1);即用str1给str2赋值a.assign(b,i,n); // 把b[i..i+n-1]赋值给a
如果直接用“=”赋值将会两者指针指向同一个地方,使用strcpy是出现不知名错误。“no matching”,copy也不能用。-------插入函数:insert();
//#include "stdafx.h"//If the vc++6.0, with this line.
#include <string>
#include <iostream>
using
namespace
std;
//
int
main(
void
){
string a=
"1234567890"
,b=
"abcdefghijklmn"
;
a.insert(3,b,5,4);
cout << a << endl;
return
0;
}
输出是123fghi4567890.
--------总结:有许多函数不清楚用法的时候先测试函数是否使用得和自己想象的一样效果,否则只会做无用功。
std::string encrypt(std::string text, int n){ std::string tmp; std::string tmp_1; for(int i=0;i<n;i++) { tmp_1.assign(text); tmp.erase(0,tmp.length()); for(int j=1;j<=text.length()/2;j++) { tmp+=tmp_1[j]; tmp_1.erase(j,1); } text=tmp+tmp_1; } return text;}std::string decrypt(std::string encryptedText, int n){std::string tmp_2;std::string tmp_3;int boundedNumber=encryptedText.length()/2; for(int i=0;i<n;i++) {tmp_2.assign(encryptedText,0,boundedNumber);tmp_3.assign(encryptedText,boundedNumber,encryptedText.length()); for(int j=0;j<boundedNumber;j++) { tmp_3.insert(2*j+1,tmp_2,j,1); } encryptedText=tmp_3; } return encryptedText;}
0 0
- 算法练习1:Take every 2nd char from the string. Then the other chars 随笔
- make the objectives then take action
- Get the solution from the other source.
- please take me away from the world
- nd the $PHP_AUTOCONF environment variable. Then, rerun this script.
- Take that chance from the book named the soap
- php 怎样去掉一个string 后面的一个或多个字符(how to remove the last n chars from a string)?
- A Voice from the Other Side
- string rotation (if string is the rotate of the other)
- You can't take the sky from me
- Take screenshots from the command line in Linux
- java中String的常用方法 1、length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars);
- Advice From An Old Programmer(Learn Python The Hard Way, 2nd Edition)
- get number from the string
- Take back the web
- C - The C Answer (2nd Edition) - Exercise 1-2
- C - The C Answer (2nd Edition) - Exercise 1-1
- C - The C Answer (2nd Edition) - Exercise 1-3
- 算法导论-学习笔记(2)
- CentOS7/rhel7 T440P 控制散热风扇转速等级
- 基于XCode8.x创建ios项目
- java 保留字符串数字的位数,不够前面补0
- linux系统文件
- 算法练习1:Take every 2nd char from the string. Then the other chars 随笔
- Android自定义View--仿驾考宝典显示分数效果
- PowerDesigner生成的ORACLE 建表脚本中去掉小写对象的引号
- windows Server 2008远程桌面服务的配置及授权
- iOS 绘制虚线的三种方法
- 面试常考数据结构算法
- 走进AngularJs(八) ng的路由机制
- GCD
- 球员能力图(综合练习)