C/C++中怎的从字符串中查找特定的字符
C/C++中怎样从字符串中查找特定的字符
比如字符串 "0.1, 9.735e-012, 4.36397e-012, 6.37811e-012, 7.72087e-012, 1.17491e-011, 5.03535e-012"怎样把其中的","(逗号)都找出来,并计算出个数??
菜鸟新人,求大神指点??
------解决思路----------------------
strchr
strstr
------解决思路----------------------
你可以自己编写一个函数啊,遍历以便就OK了。
------解决思路----------------------
统计个数(方法一):
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string st("0.1, 9.735e-012, 4.36397e-012, 6.37811e-012, 7.72087e-012, 1.17491e-011, 5.03535e-012");
int nCount = count(st.begin(), st.end(), ',');
}
统计个数(方法二)并记录所有逗号','
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string st("0.1, 9.735e-012, 4.36397e-012, 6.37811e-012, 7.72087e-012, 1.17491e-011, 5.03535e-012");
vector<string::iterator> vec_it;
string::iterator it_start = st.begin();
while(it_start != st.end())
{
it_start = find(it_start, st.end(), ',');
if (it_start != st.end())
{
vec_it.push_back(it_start);
}
}
int nCount = vec_it.size(); // 统计个数, 并且此时所有的逗号','的迭代器都被存储在vec_it容器中
}
------解决思路----------------------
strchr或者自己遍历都可以呀
------解决思路----------------------
直接FIND函数就行了啊
------解决思路----------------------
------解决思路----------------------
感觉还是for循环 挨个比较以下,比较好控制
------解决思路----------------------
历遍数组,然后统计个数
------解决思路----------------------
有时候想一想,他妈编程真难,不过像通了的时候有觉得,就那么回事。
------解决思路----------------------
翻过了山就不觉得山高
比如字符串 "0.1, 9.735e-012, 4.36397e-012, 6.37811e-012, 7.72087e-012, 1.17491e-011, 5.03535e-012"怎样把其中的","(逗号)都找出来,并计算出个数??
菜鸟新人,求大神指点??
------解决思路----------------------
strchr
strstr
------解决思路----------------------
你可以自己编写一个函数啊,遍历以便就OK了。
------解决思路----------------------
统计个数(方法一):
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string st("0.1, 9.735e-012, 4.36397e-012, 6.37811e-012, 7.72087e-012, 1.17491e-011, 5.03535e-012");
int nCount = count(st.begin(), st.end(), ',');
}
统计个数(方法二)并记录所有逗号','
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string st("0.1, 9.735e-012, 4.36397e-012, 6.37811e-012, 7.72087e-012, 1.17491e-011, 5.03535e-012");
vector<string::iterator> vec_it;
string::iterator it_start = st.begin();
while(it_start != st.end())
{
it_start = find(it_start, st.end(), ',');
if (it_start != st.end())
{
vec_it.push_back(it_start);
}
}
int nCount = vec_it.size(); // 统计个数, 并且此时所有的逗号','的迭代器都被存储在vec_it容器中
}
------解决思路----------------------
strchr或者自己遍历都可以呀
#include <stdio.h>
#include <string.h>
void
demo(const char *s)
{
int n;
char c;
const char *p;
c = ',';
n = 0;
for (p = s; *p; ++p) {
if (*p == c) {
printf("p=%d\n", (int)(p - s));
++n;
}
}
printf("n=%d\n", n);
}
int
main(int argc, char *argv[])
{
char s [] =
"0.1, 9.735e-012, 4.36397e-012, 6.37811e-012, 7.72087e-012, 1.17491e-011, 5.03535e-012"
;
char *p;
char c;
int n;
c = ',';
n = 0;
for (p = s; p = strchr(p, c); ) {
printf("p=%d\n", (int)(p - s));
++n;
++p;
}
printf("n=%d\n", n);
printf("%s\n", "====");
demo(s);
return 0;
}
------解决思路----------------------
直接FIND函数就行了啊
------解决思路----------------------
#include <stdio.h>
char *s="0.1, 9.735e-012, 4.36397e-012, 6.37811e-012, 7.72087e-012, 1.17491e-011, 5.03535e-012";
int strnchr(const char *p,const char c) {
int n;
n=0;
while (1) {
if (0==*p) break;
if (c==*p) n++;
p++;
}
return n;
}
int main() {
printf("%d\n",strnchr(s,','));//6
return 0;
}
------解决思路----------------------
感觉还是for循环 挨个比较以下,比较好控制
------解决思路----------------------
历遍数组,然后统计个数
------解决思路----------------------
有时候想一想,他妈编程真难,不过像通了的时候有觉得,就那么回事。
/*************************************************************************
> File Name: char_count.cpp
> Author: Jukay
> Mail: hellojukay@gmail.com
> Created Time: 2014年10月29日 星期三 19时49分21秒
************************************************************************/
#include<iostream>
#include<string>
using namespace std;
int main()
{
string test ="0.1, 9.735e-012, 4.36397e-012, 6.37811e-012, 7.72087e-012, 1.17491e-011, 5.03535e-012";
int num=0;
for(auto c : test )
{
if(c == ',')
num++;
}
cout<< num << endl;
return 0;
}
------解决思路----------------------
翻过了山就不觉得山高