C++ 字符串 技术教程文章

C++拆分带空格的字符串——HDU 2072题解【代码】

【题目描述】 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。 【Input】 有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。 【Output】 每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。 【Sample Input】 you are my friend # 【Sample ...

字符串压缩的C++代码【代码】

to_string方法使用不了时 #include <iostream> #include <string> #include <sstream> using namespace std;string compress(string iniString) {string str;int count=1;for(int i=0;i<iniString.length();i++){if(iniString[i]==iniString[i+1]){count++;continue;}stringstream ss;//使用stringstream实现int转stringss<<count;string s1=ss.str(); str+=iniString[i];str+=s1;count=1;} // if(str==iniString) // {return iniS...

统计字符串字符空格数C++【代码】

#include <iostream> #include <string.h> using namespace std;int main() {char ch[100];gets(ch);//getchar();int len=strlen(ch);int count=0;int space=0;for(int i=len-1;i>=0;i--){cout<<ch[i];}cout<<endl;2for(int j=0;j<len;j++){if(ch[j]==' '){++space;}else{++count;}}cout<<count<<endl;cout<<space<<endl;cout<<endl;;return 0;}

字符串大小写转换C++【代码】

#include <iostream> #include <string.h> using namespace std;int main() {char ch[100];gets(ch);int len=strlen(ch);for(int i=0;i<len;i++){if(ch[i]>='A' && ch[i]<='Z'){ch[i]=ch[i]+32;}else if(ch[i]>='a' && ch[i]<='z'){ch[i]=ch[i]-32;}cout<<ch[i];} return 0; }

C++:反转字符串【代码】

class Solution { public:string reverseString(string s) {if(s[0] == NULL)//空返回return s;int begin = 0;//设置头指针int end = s.size() - 1;//设置尾指针while(begin < end){swap(s[begin], s[end]);begin++;end--;}return s;} };思路:设置头尾两个指针,两个指针交换,然后头++,尾–,两个指针交替或者相等的时候跳出循环,返回第一位数的下标。

C++ string 字符串函数详解

C++ string 字符串函数详解 原文地址:https://www.renfei.org/blog/introduction-to-cpp-string.html 运算符重载 + 和 +=:连接字符串 =:字符串赋值 >、>=、< 和 <=:字符串比较(例如a < b, aa < ab) ==、!=:比较字符串 <<、>>:输出、输入字符串 注意:使用重载的运算符 + 时,必须保证前两个操作数至少有一个为 string 类型。例如,下面的写法是不合法的:#include <iostream> #include <string> int main() {string str = ...

设计一个字符串类String(C++练习题)【代码】

要求:设计一个字符串类String,可以求字符串长度,可以连接两个串(如,s1=“计算机”,s2=“软件”,s1与s2连接得到“计算机软件”),并且重载“=”运算符进行字符串赋值,编写主程序实现:s1="计算机科学",s2=“是发展最快的科学!”,求s1和s2的串长,连接s1和s2 #include "stdafx.h" #include <iostream> #include <string> using namespace std;class String {char str[255];public:String(){str[0]=\0;}String(char* s){...

分割字符串(C++)【图】

方案1: 利用"IO流"的概念,即C++中的stream,我们都用过C++中std::iostream中的std::istream与std::ostream如果你接触过网络编程(Socket编程),可能会对这个流的概念更加清楚。在C++中,我们常用的cin其实是一个istream对象,从标准输入读取数据,cout是一个ostream对象,用于向标准输出写入数据。IO对象无拷贝或赋值。 相应的,我们可以使用std::istream_iterator来作为关联输入流的迭代器: std::string text = "Let me split thi...

字符串转整型C++【代码】

#include<iostream> #include<sstram> using namespace std;int myAtoi(string str) {string s="";int result;bool flag=true;for(int i=0; i<str.length(); i++) {if(! ((str[i] >= 0 && str[i] <=9) || str[i] ==-|| str[i] == || str[i] == +) ){break;}if(str[i] == && flag){continue;}else{flag= false;s+=str[i];}}int Int; // stringstream ss; // ss << s; // ss >> Int;char st[1000000];for(int j=0; j<s.le...

字符串的输入问题 C++【代码】

C++中,初学时最常用的输入字符的方式为cin,但是,cin是如何确定已经完成了字符串的输入了呢?由于不能通过键盘输入空字符("\0"),因此cin需要用别的方法来确定字符串的结尾位置。cin使用空白(空格,制表符和换行符)来确定字符串结束的位置,这意味着cin在取字符数组输入时只能读取一个单词。读取该单词后,cin将该字符串放到数组中,并自动在结尾添加空字符。如下的程序所示:#include<iostream> using namespace std; int main...

c++实现字符串中空格的替换【代码】【图】

题目描述:请实现一个函数,把字符串中的每个空格替换成“%20”。例如,输入“We are happy.”,则输出“We%20are%20happy.”。 看到这个题目的时候我们首先想到的是,从前向后遍历,每找到一个空格就把这个空格替换成%20,但是空格只有一个字符,%20 有三个字符,这样替换的时候势必要将数组中的元素向后移动。而且有些字符还被不止移动一次, 假设字符串长度是n,对每个空格字符,需要移动后面O(n)个字符,因此对于含有O(n)个空格...

C++学习笔记6_字符串

1. C语言的字符串,char * s = "aaaa"; #include<string> class Test{ public : Test(int a, char*name) { this->a=a; int len = strlen(name); this->name=new char[len+1]; strcpy(this->name,name); } ~Test() { if(this->name!=NULL) { delete name; name =NULL; } } private: int a; char* name;}// strlen("123")只是字符串的有效长度,不是实际长度,实际是"123\0",区别于sizeof("123"),sizeof只是数据类型的...

寻找字符串中最后一个只出现一次的字符(华为笔试题 C++实现)

找出给定字符串中最后一个只出现一次的字符,如字符串abbcddefgghh,输出结果为f。 借助C++ map容器实现,主要思路是:遍历字符串中每个字符元素,通过map容器记录每个字符出现的次数,然后在map容器中遍历找到最后一个字符出现次数为1的那个字符。#include <iostream> #include <string> #include <unordered_map> using namespace std; int main() {string str;getline(cin, str);unordered_map<char, int> mp;string::iterator ...

P3880[JLOI2008]提示问题 洛谷 (C++)(模拟)(字符串处理)

题意很好理解,但这道题实在太考细节了... 随便乱打的一个测试样例竟然帮我找到第八组测试样例过不去的原因??hhhh... 有个小地方忘写了导致第十组样例一直WA... 为各位dalao献上蒟蒻的代码,详情请看注释。#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <map> #include <cmath> #include <queue> #include <cmath> #include <ctype.h> #define ll long longusing namespace std ;str...

二维数组求每名学生的总成绩和平均成绩(c++中字符串的输出)

#include “stdafx.h” #include #include //可以输出字符串 using namespace std; int main() { int scores[3][4]; cout << “请输入同学姓名以及成绩!”<<endl; for (int i =0; i ??; i++) { for (int j = 0; j < 4; j++) { cin >> scores[i][j]; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { string t; if (j == 0) { t = “语文”; } else if (j == 1) { t = “数学”; } else { t = “英语”; } cout<...

P1781 宇宙总统 洛谷 (C++)(字符串)(排序)【代码】

第一种方法是构建一个空字符串maxx,每次和输入的字符串比较,找到最大的则更新maxx串,然后更新位置信息。 所谓最大就是 输入的字符串的长度大于maxx的长度或者在长度相等的情况下比较字典序的大小。最后输出信息即可。 详情见代码#include <iostream> #include <cstdio> #include <algorithm> #define ll long longusing namespace std ;int main(){int n ;cin >> n ;string str ;string maxx = "" ;int pos = -1 ;for ( int i =...

C++切割字符串

将类似 1,2,3,4,5,6 2提取到vector内 int main() { char line[1000001]; char str[1000]; while (cin.getline(line, 1000000)) { vector<int> vec; int n = 0; sscanf(line, "%s %d", str, &n); char *p = strtok(cstr, ","); while (p) { int num; sscanf(p, "%d", &num); vec.push_back(num); p = strtok(NULL, ...