C编程中的字符串拆分

C编程中的字符串拆分,c,linux,string,C,Linux,String,我需要拆分一个字符串,并需要存储在两个独立的变量中。该字符串包含一个选项卡空间。因此,它需要与选项卡空间分开 这根绳子看起来像这样 索尼有一个印地语频道。 我需要将Sony存储在一个变量中,比如chara[6]和另一个变量中的印地语通道,比如字符b[20] 如何做到这一点?可能功能是您正在查看我的C很旧,但类似的功能应该可以工作: #include <stdio.h> int getTabPosition (char str []) { int i = 0; /

我需要拆分一个字符串,并需要存储在两个独立的变量中。该字符串包含一个选项卡空间。因此,它需要与选项卡空间分开

这根绳子看起来像这样

索尼有一个印地语频道。 我需要将
Sony
存储在一个变量中,比如
chara[6]
和另一个变量中的
印地语通道
,比如
字符b[20]


如何做到这一点?

可能功能是您正在查看

我的C很旧,但类似的功能应该可以工作:

#include <stdio.h>


int getTabPosition (char str [])
{
    int i = 0;
    //While we didn t get out of the string
    while (i < strlen(str))
    {
        //Check if we get TAB
        if (str[i] == '\t')
            //return it s position
            return i;
        i = i + 1;
    }
    //If we get out of the string, return the error
    return -1;
}

int main () {
    int n = 0;
    //Source
    char str [50] = "";
    //First string of the output
    char out1 [50] = "";
    //Second string of the output
    char out2 [50] = "";

    scanf(str, "%s");
    n = getTabPosition(str);
    if (n == -1)
        return -1;
    //Copy the first part of the string
    strncpy(str, out1, n);
    //Copy from the end of out1 in str to the end of str
    //str[n + 1] to skip the tab
    memcpy(str[n+1], out2, strlen(str) - n);
    fprintf(stdout, "Original: %s\nout1=%s\nout2=%s", str, out1, out2);
    return 0;
}
#包括
int getTabPosition(char str[])
{
int i=0;
//虽然我们没有摆脱束缚
而(i

未经测试,但许多编程语言都有标记化字符串的原则:

在您的情况下,是一个特殊字符,可以表示为“\t”

如果您使用的是C编程语言

#include<string.h>
#include<stdio.h>
#include<stdlib.h>

int main(void) {
  char *a[5];
  const char *s="Sony\tA Hindi channel.";
  int n=0, nn;

  char *ds=strdup(s);

  a[n]=strtok(ds, "\t");
  while(a[n] && n<4) a[++n]=strtok(NULL, "\t");

  // a[n] holds each token separated with tab

  free(ds);

  return 0;
}
#包括
#包括
#包括
内部主(空){
char*a[5];
const char*s=“索尼\tA印地语频道。”;
int n=0,nn;
char*ds=strdup(s);
a[n]=strtok(ds,“\t”);
while(a[n]&&n标记(文本,sep);
BOOST_FOREACH(常量字符串和t、令牌){

CUT是C++还是C?请您决定保留制表符,即“<代码> fo\tbar >代码>导致<代码> fo\t\/c>和<代码> bar <代码>或<代码> fo> <代码>和<代码> bar >代码>如果有两个后续分隔符,如代码> fo\tbbar < /代码>。若要从源字符串复制字符直到看到选项卡,请跳过它,然后将其余的字符复制到另一个字符串中。您应该先查看strok()并自己做一些工作。如果在使其正常工作时遇到问题,请将代码发布到此处。这不值得计算
strlen(str)
while
循环中的每次迭代中,可以是
while(str[i]!=(char)0)
@BasileStarynkevitch:我没有想到,我已经用你的代码编辑了。谢谢提醒。
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>

int main() {
  std::string s = "Sony\tA Hindi channel.";
  std::vector<std::string> v;
  std::istringstream buf(s);
  for(std::string token; getline(buf, token, '\t'); )
      v.push_back(token);
  // elements of v vector holds each token
}
#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>

using namespace std;
using namespace boost;

int main(int, char**) {
  string text = "Sony\tA Hindi channel.";

  char_separator<char> sep("\t");
  tokenizer< char_separator<char> > tokens(text, sep);
  BOOST_FOREACH (const string& t, tokens) {
      cout << t << "." << endl;
  }
}