字符串分隔split

按点对字符串进行分隔。题如下:

方法1:

#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
using namespace std;


int main()
    {
   
   
     string s;
     while(cin>>s)
         {  
          int a;
    vector<string> vs;
         boost::split( vs, s, boost::is_any_of( "." ), boost::token_compress_on ); 
    bool flag=true;
         for(int i=0;i<vs.size();i++)
        { 
           int t=atoi(vs[i].c_str()) ;
        if(t<0||t>255)
            {
            cout<<"NO"<<endl;
            flag=false;
            //return 0;//不能连续输入
        }
           
    }
          if(flag)
     cout<<"YES"<<endl;
         }
   
          return 0;
    
}


方法2:

#include<iostream>
#include<string>
#include<string.h>


using namespace std;


int main()
    {
   
     char a[900];
     int k=0;
     while(cin>>a)
         {      bool flag=true;
         char* ptr;
         ptr = strtok(a, ".");  
   // while(ptr != NULL)
          while(ptr = strtok(NULL, "."))
          {  
      int t=atoi(ptr) ;
        if(t<0||t>255)
            {
            cout<<"NO"<<endl;
            flag=false;
            
            //return 0;//不能连续输入
        }
       // ptr = strtok(NULL, ".");  
    } 
         


     
          if(flag)
     cout<<"YES"<<endl;
         }
   
          return 0;
    
}

题目描述

现在IPV4下用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此不需要用正号出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。

现在需要你用程序来判断IP是否合法。



输入描述:

输入一个ip地址



输出描述:

返回判断的结果YES or NO


输入例子:
10.138.15.1

输出例子:
YES
字符串分隔大全:

http://www.jb51.net/article/55954.htm 经典

本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考。具体分析如下:

我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用。

一、用strtok函数进行字符串分割

原型: char *strtok(char *str, const char *delim);

功能:分解字符串为一组字符串。

参数说明:str为要分解的字符串,delim为分隔符字符串。

返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。

其它:strtok函数线程不安全,可以使用strtok_r替代。

示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//借助strtok实现split
#include <string.h>
#include <stdio.h>
  
int main()
{
     char s[] = "Golden Global   View,disk * desk" ;
     const char *d = " ,*" ;
     char *p;
     p = strtok (s,d);
     while (p)
     {
         printf ( "%s\n" ,p);
         p= strtok (NULL,d);
     }
  
     return 0;
}

运行效果如下图所示:

二、用STL进行字符串的分割

涉及到string类的两个函数find和substr:
1、find函数
原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置。
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos

2、substr函数
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:获得子字符串。
参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)
返回值:子字符串

实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//字符串分割函数
std::vector<std::string> split(std::string str,std::string pattern)
{
   std::string::size_type pos;
   std::vector<std::string> result;
   str+=pattern; //扩展字符串以方便操作
   int size=str.size();
  
   for ( int i=0; i<size; i++)
   {
     pos=str.find(pattern,i);
     if (pos<size)
     {
       std::string s=str.substr(i,pos-i);
       result.push_back(s);
       i=pos+pattern.size()-1;
     }
   }
   return result;
}

完整代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
     File   : split1.cpp
     Author  : Mike
     E-Mail  : Mike_Zhang@live.com
  */
#include <iostream>
#include <string>
#include <vector>
  
//字符串分割函数
std::vector<std::string> split(std::string str,std::string pattern)
{
   std::string::size_type pos;
   std::vector<std::string> result;
   str+=pattern; //扩展字符串以方便操作
   int size=str.size();
  
   for ( int i=0; i<size; i++)
   {
     pos=str.find(pattern,i);
     if (pos<size)
     {
       std::string s=str.substr(i,pos-i);
       result.push_back(s);
       i=pos+pattern.size()-1;
     }
   }
   return result;
}
  
int main()
{
   std::string str;
   std::cout<< "Please input str:" <<std::endl;
   //std::cin>>str;
   getline(std::cin,str);
   std::string pattern;
   std::cout<< "Please input pattern:" <<std::endl;
   //std::cin>>pattern;
   getline(std::cin,pattern); //用于获取含空格的字符串
   std::vector<std::string> result=split(str,pattern);
   std::cout<< "The result:" <<std::endl;
   for ( int i=0; i<result.size(); i++)
   {
     std::cout<<result[i]<<std::endl;
   }
  
   std::cin.get();
   std::cin.get();
   return 0;
}

运行效果如下图所示:



三、用Boost进行字符串的分割

用boost库的正则表达式实现字符串分割
实现如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
std::vector<std::string> split(std::string str,std::string s)
{
     boost::regex reg(s.c_str());
     std::vector<std::string> vec;
     boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);
     boost::sregex_token_iterator end;
     while (it!=end)
     {
         vec.push_back(*it++);
     }
     return vec;
}

完整代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//本程序实现的是利用正则表达式对字符串实现分割
//运行环境   VC6.0 + boost 库
/*
     File   : split2.cpp
     Author  : Mike
     E-Mail  : Mike_Zhang@live.com
*/
#include <iostream>
#include <cassert>
#include <vector>
#include <string>
#include "boost/regex.hpp"
  
std::vector<std::string> split(std::string str,std::string s)
{
     boost::regex reg(s.c_str());
     std::vector<std::string> vec;
     boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);
     boost::sregex_token_iterator end;
     while (it!=end)
     {
         vec.push_back(*it++);
     }
     return vec;
}
int main()
{
     std::string str,s;
     str= "sss/ddd/ggg/hh" ;
     s= "/" ;
     std::vector<std::string> vec=split(str,s);
     for ( int i=0,size=vec.size();i<size;i++)
     {
         std::cout<<vec[i]<<std::endl;
     }
     std::cin.get();
     std::cin.get();
     return 0;
}

运行效果如下图所示:

补充:

最近发现boost里面有自带的split的函数,如果用boost的话,还是直接用split的好,这里就不多说了,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
   
using namespace std;
   
int main()
{
  string s = "sss/ddd,ggg" ;
  vector<string> vStr;
  boost::split( vStr, s, boost::is_any_of( ",/" ), boost::token_compress_on );
  for ( vector<string>::iterator it = vStr.begin(); it != vStr.end(); ++ it )
   cout << *it << endl;
  return 0;
}

 

strtok研究:


http://c.biancheng.net/cpp/html/175.html

分解字符串为一组字符串。s为要分解的字符,delim为分隔符字符(如果传入字符串,则传入的字符串中每个字符均为分割符)。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。

strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果要保持原字符串的完整,可以使用strchr和sscanf的组合等。

http://baike.baidu.com/link?url=mDWzem0Oqq4fOrlCr7yvMHVq2EIbwhHuxFW_477lhOzYjAwuGMbXbab0jnzMVLpX8c94AnWYKqfAw2D_l2_MCa

http://blog.csdn.net/hustfoxy/article/details/23473805/

http://bbs.csdn.net/topics/40226502

#include <string.h>
#include<stdio.h>
int main(){
char s[] = "ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z 和";
char *delim = "-: ";
char *p;
//p = strtok(s, delim);
printf("%s ", strtok(s, delim));
while ((p = strtok(NULL, delim)))
printf("%s ", p);
printf("\n");
}




#include <string.h>
#include<stdio.h>
int main(){
char s[] = "ab-cd : ef;gh :i-jkl;mnop;qrs-tu: vwx-y;z 和";
char *delim = "-: ";
char *p;
p = strtok(s, delim);
//printf("%s ", strtok(s, delim));
while (p != NULL)
{
printf("%s ", p);
p = strtok(NULL, delim);

}

printf("\n");
}


ab cd ef;gh i jkl;mnop;qrs tu vwx y;z 和
请按任意键继续. . .


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C语言中并没有提供直接的字符串分割函数,但可以使用循环和指针等基本操作实现字符串的分割。一般可以采用以下步骤: 1.定义一个字符串数组,用来存储分割后的字符串。例如:char split_str[10][20],表示最多分割10个长度为20的字符串。 2.定义一个指向分割后字符串数组的指针,例如:char (*p)[20] = split_str。 3.定义一个分隔符,例如:char separator='-'。 4.使用strtok函数分割字符串:char *token = strtok(str, &separator),其中str是要分割的字符串。 5.循环使用strtok函数获取剩下的字符串,然后将其存入分割后字符串数组中。例如: while(token != NULL){ strcpy(*p, token); p++; token = strtok(NULL, &separator); } 6.最终得到的分割后字符串数组split_str即为所求。 需要注意的是,分割字符串的同时会改变原字符串,因此不能直接使用原字符串,需要使用另一个字符串变量来进行操作。同时,分割后的字符串数组中的每个字符串需要用strcpy函数进行复制,否则会出现指针指向同一内存地址的问题导致结果错误。 ### 回答2: C字符串数组中的split函数(也称为分裂函数)可以将一个字符串分成多个部分。这个函数通过定义一个分隔符来识别字符串中的不同部分,并将它们存储到不同的数组元素中。这个分隔符可以是单个字符,也可以是字符串。 C字符串数组的split函数的语法格式为: char **split(char *str, const char *delim); 这个函数接受两个参数。第一个参数str是要分裂的字符串,第二个参数delim是用来分隔字符串分隔符。该函数返回一个指向指针数组的指针。这个指针数组包含分隔后的每个部分。 举个例子,假设有这样一个字符串: char str[] = "apple,banana,orange"; 要将这个字符串分裂成三个部分,可以使用逗号作为分隔符,例如: char **result = split(str, ","); 这个函数将返回一个指向指针数组的指针。使用for循环可以遍历这个数组,并将分隔后的每个部分打印出来,例如: for(int i=0; result[i]!=NULL; i++) { printf("%s\n", result[i]); } 这样就可以将分隔后的每个部分分别打印出来,即: apple banana orange 通过这个例子可以看出,C字符串数组的split函数非常有用,可以帮助我们轻松地分隔字符串并将分隔后的每个部分存储到数组中。 ### 回答3: C语言中的字符串数组split指的是将一个字符串按照指定的分隔符进行分割,将分割后的子串存储到一个字符串数组中。这个字符串数组可以使用数组或者动态内存分配进行定义。 split函数通常包含三个参数:被分割的字符串分隔符和目标存储分割结果的字符串数组。函数内部会遍历被分割字符串,按照分隔符将其分割成多个子串,并将这些子串存储到目标字符串数组中。 在实现上,可以使用循环和指针来实现字符串的分割和存储。具体过程如下:首先在主函数中定义一个目标字符串数组。在split函数中,使用指针遍历被分割字符串,当找到分隔符时,将分割前的子串存储到目标数组中,并将指针移动到下一个子串的开头,重复这个过程直到整个字符串被分割完。 需要注意的是,C语言中的字符串数组不会自动调整大小,因此在定义目标字符串数组时需要考虑存储空间的大小,避免发生数组越界等错误。 总之,split函数是C语言中常见的字符串操作函数,可以用于将一个字符串分割成多个子串,方便后续处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值