C 如何使用分隔符进行分析
我有一行,格式如下:C 如何使用分隔符进行分析,c,string,C,String,我有一行,格式如下: str="WORD1\tWORD2\tWORD3\tWORD4..."; 问题是我需要得到第三个词 我一直在玩strchr和strcpy,但我只是把事情搞得一团糟 我想做的唯一一件事就是保存在同一个字符串变量str中,这样我就可以一直解析到第三列 char *p; p=strchr(str,'\t'); strcpy(str,str,p?); 谢谢你的帮助 谢谢 有 strpbrk() strspn() strtok() strsep() // define _BSD
str="WORD1\tWORD2\tWORD3\tWORD4...";
问题是我需要得到第三个词
我一直在玩strchr和strcpy,但我只是把事情搞得一团糟
我想做的唯一一件事就是保存在同一个字符串变量str中,这样我就可以一直解析到第三列
char *p;
p=strchr(str,'\t');
strcpy(str,str,p?);
谢谢你的帮助
谢谢 有
strpbrk()
strspn()
strtok()
strsep() // define _BSD_SOURCE to have this available
可以进行解析
您还可以通过调用
regcomp()
和regexec()
使用正则表达式
strpbrk()
strspn()
strtok()
strsep() // define _BSD_SOURCE to have this available
可以进行解析
您还可以通过调用
regcomp()
和regexec()
来使用正则表达式
strchr
将定位字符串()中第一个出现的字符
(2)
strcpy
将源指向的字符串复制到目标指向的数组()
(3)
您需要使用strtok
来解析输入字符串()
请参见一个示例:
(4)
不能将字符串复制到未分配的指针中。您需要为目标分配内存
(5)
下面是一个示例代码,可以为您提供更多解释:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str="WORD1\tWORD2\tWORD3\tWORD4...";
char *p = NULL;
char *out = NULL;
int count = 0;
int needed = 2;
p = strtok(str , "\t");
while(p && count != needed) {
p = strtok(NULL , "\t");
count ++;
}
if(p) {
out = malloc(strlen(p) + 1);
strcpy(out, p);
printf("String found: %s\n", out);
}
return 0;
}
#包括
#包括
#包括
int main(){
char*str=“WORD1\tWORD2\tWORD3\tWORD4…”;
char*p=NULL;
char*out=NULL;
整数计数=0;
int=2;
p=strtok(str,“\t”);
while(p&&count!=需要){
p=strtok(空,“\t”);
计数++;
}
如果(p){
out=malloc(strlen(p)+1);
strcpy(out,p);
printf(“找到的字符串:%s\n”,输出);
}
返回0;
}
(1)
strchr
将定位字符串()中第一个出现的字符
(2)
strcpy
将源指向的字符串复制到目标指向的数组()
(3)
您需要使用strtok
来解析输入字符串()
请参见一个示例:
(4)
不能将字符串复制到未分配的指针中。您需要为目标分配内存
(5)
下面是一个示例代码,可以为您提供更多解释:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str="WORD1\tWORD2\tWORD3\tWORD4...";
char *p = NULL;
char *out = NULL;
int count = 0;
int needed = 2;
p = strtok(str , "\t");
while(p && count != needed) {
p = strtok(NULL , "\t");
count ++;
}
if(p) {
out = malloc(strlen(p) + 1);
strcpy(out, p);
printf("String found: %s\n", out);
}
return 0;
}
#包括
#包括
#包括
int main(){
char*str=“WORD1\tWORD2\tWORD3\tWORD4…”;
char*p=NULL;
char*out=NULL;
整数计数=0;
int=2;
p=strtok(str,“\t”);
while(p&&count!=需要){
p=strtok(空,“\t”);
计数++;
}
如果(p){
out=malloc(strlen(p)+1);
strcpy(out,p);
printf(“找到的字符串:%s\n”,输出);
}
返回0;
}
试试这个,只是一个想法(我没有检查其他字符串):
代码不给出单词的首字母和末字母,它只输出子字符串,如果它的父字符串是由\t
根据需要指定的:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char* str="WORD1\tWORD2\tWORD3\tWORD4";
char *p1 = NULL;
char *p2 = NULL;
int i = 0, which = 3;
char word[10]={0};
scanf("%d", &which );
p1 = str;
for(i=0 ; (p1 != NULL) && (i < which-1); i++){
p1=strchr(p1 + 1, '\t');
if(p1 +1)
p2=strchr(p1 + 1, '\t');
}
if(p1!=NULL && p2!=NULL){
strncpy(word, p1, p2-p1);
printf("\n %s\n", word);
}
return 1;
}
这是您需要的吗?试试这个,只是一个想法(我没有检查其他字符串):
char *cut(int nth, char* out, const char* in){
const char *pi=in;
char *po=out;
int n = 0;
for(;;){
*po=*pi;
if(*po=='\t')
*po='\0';
if(*po == '\0'){
++n;
if(n==nth)
return out;
else if(*pi == '\0')break;
po=out;
} else
++po;
++pi;
}
return NULL;
}
/*
int main() {
const char *str="WORD1\tWORD2\tWORD3\tWORD4\tWORD5";
char word[32]={0};
printf("%s\n", cut(3, word, str));//WORD3
return 0;
}
*/
代码不给出单词的首字母和末字母,它只输出子字符串,如果它的父字符串是由\t
根据需要指定的:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char* str="WORD1\tWORD2\tWORD3\tWORD4";
char *p1 = NULL;
char *p2 = NULL;
int i = 0, which = 3;
char word[10]={0};
scanf("%d", &which );
p1 = str;
for(i=0 ; (p1 != NULL) && (i < which-1); i++){
p1=strchr(p1 + 1, '\t');
if(p1 +1)
p2=strchr(p1 + 1, '\t');
}
if(p1!=NULL && p2!=NULL){
strncpy(word, p1, p2-p1);
printf("\n %s\n", word);
}
return 1;
}
这是您所需要的吗?很抱歉耽搁了您的时间,这就是我最终实现它的方式:
char *cut(int nth, char* out, const char* in){
const char *pi=in;
char *po=out;
int n = 0;
for(;;){
*po=*pi;
if(*po=='\t')
*po='\0';
if(*po == '\0'){
++n;
if(n==nth)
return out;
else if(*pi == '\0')break;
po=out;
} else
++po;
++pi;
}
return NULL;
}
/*
int main() {
const char *str="WORD1\tWORD2\tWORD3\tWORD4\tWORD5";
char word[32]={0};
printf("%s\n", cut(3, word, str));//WORD3
return 0;
}
*/
int main()
{
char *readbuff="WORD1\tWORD2\tWORD3\tWORD4...";
char * pch;
int third_col=0;
pch = strtok (readbuff," \t");
while ((pch != NULL)&& (TRUE == notfound))
{
pch = strtok (NULL, " \t");
if (third_col==1)
{
dprintf (DBG_INFO,"The third word is: %s\n",pch);
notfound=FALSE;
}
third_col++;
}
}
很抱歉耽搁了,这是我最终实现它的方式:
int main()
{
char *readbuff="WORD1\tWORD2\tWORD3\tWORD4...";
char * pch;
int third_col=0;
pch = strtok (readbuff," \t");
while ((pch != NULL)&& (TRUE == notfound))
{
pch = strtok (NULL, " \t");
if (third_col==1)
{
dprintf (DBG_INFO,"The third word is: %s\n",pch);
notfound=FALSE;
}
third_col++;
}
}
简单C++代码:
string s = "abc\tdef\tghi";
stringstream ss(s);
string a,b,c;
ss >> a ; ss.ignore() ; ss >> b ; ss.ignore() ; ss >> c;
cout << a << " " << b << " " << c << endl;
简单C++代码:
string s = "abc\tdef\tghi";
stringstream ss(s);
string a,b,c;
ss >> a ; ss.ignore() ; ss >> b ; ss.ignore() ; ss >> c;
cout << a << " " << b << " " << c << endl;
可能重复的字符*p;p=strchr(str,'\t');p=strchr(p+1,'\t');p=strchr(p+1,'\t');这是没有做任何错误检查。strchr将返回指针指向下一次出现的指针,如果指针到达字符串末尾,则返回NULL,因此您需要增加指针以搜索另一个“\t”可能重复的char*p;p=strchr(str,'\t');p=strchr(p+1,'\t');p=strchr(p+1,'\t');这是没有做任何错误检查。strchr将返回指针指向下一次出现的指针,如果指针到达字符串末尾,则返回NULL,因此您需要增加指针以搜索另一个非常有用的'\t'。我将在几个小时后发布我使用的解决方案。这非常有用。我将在几个小时后发布我使用的解决方案。我已经用非常类似的方法解决了它。我会在时间限制结束后发布它。我已经用一种非常类似的方法解决了它。我会在时间限制结束后发布。