C - 如何通过子串分割字符串[重复]

时间:2022-04-25 19:19:38

This question already has an answer here:

这个问题在这里已有答案:

this might be some basic question, but I really can't find an answer: I need to split a string that has two numbers, into two strings in C.

这可能是一些基本问题,但我真的找不到答案:我需要将一个包含两个数字的字符串拆分为C中的两个字符串。

Example:

1->2 into 1 and 2

1-> 2分为1和2

I'm using strtok, but it takes a series of chars to split, you can't specify a sequence to be the divisor.

我正在使用strtok,但它需要一系列字符才能拆分,你不能指定一个序列作为除数。

I can't use anything outside from ANSI C.

我不能使用ANSI C以外的任何东西。

Any help will be appreciated!

任何帮助将不胜感激!

How can you flag this as exact duplicate? It's not even close...

你怎么能把它标记为完全重复?它甚至没有......

3 个解决方案

#1


5  

Use 'sscanf()'. Don't ask someone to do it for you as it's too easy if you give a few minutes to understand it, especially its format specifier string nuances.Here's a link that would suffice :

使用'sscanf()'。不要让别人为你做这件事,因为如果你花几分钟时间来理解它就太容易了,特别是它的格式说明字符串细微差别。这是一个足够的链接:

http://www.cplusplus.com/reference/cstdio/sscanf/

Hint : sscanf (source_string,"%d->%d",&num1,&num2);

提示:sscanf(source_string,“%d - >%d”,&num1,&num2);

#2


2  

Well here's something to get you started:

那么这里有一些东西让你开始:

const char *FindSubString( const char *sub, const char *src )
{
  // Loop through the source string
  while(*src++)
  {
    // Only check for matching character in beginning of sub sequence
    if(*src == *sub)
    {
      return src;
    }
  }
}

This just finds a pointer to the first matching character in a sub-sequence and a source string. I'm sure you can get together something in the inner-loop that checks to see if the sub string matches the next few characters in the src string.

这只是找到指向子序列中的第一个匹配字符和源字符串的指针。我确信你可以在内循环中聚集一些东西来检查子字符串是否匹配src字符串中的下几个字符。

#3


1  

This is a brute force method. just suitable for a quick fix.

这是一种蛮力方法。只适合快速修复。

#include <stdlib.h>
#include <ctype.h>

void extract_2_numbers (char *str, int *a, int *b) {
  *a = atoi (str);
  while (isdigit(*str)) str++;
  while (!isdigit(*str)) str++;
  *b = atoi (str);
}

int main () {
  int x, y;
  char *string = "12--->24";

  extract_2_numbers (string, &x, &y);
  printf ("%d\t%d\n", x, y);
  return 0;
}

Prints this output :

打印此输出:

12    24

#1


5  

Use 'sscanf()'. Don't ask someone to do it for you as it's too easy if you give a few minutes to understand it, especially its format specifier string nuances.Here's a link that would suffice :

使用'sscanf()'。不要让别人为你做这件事,因为如果你花几分钟时间来理解它就太容易了,特别是它的格式说明字符串细微差别。这是一个足够的链接:

http://www.cplusplus.com/reference/cstdio/sscanf/

Hint : sscanf (source_string,"%d->%d",&num1,&num2);

提示:sscanf(source_string,“%d - >%d”,&num1,&num2);

#2


2  

Well here's something to get you started:

那么这里有一些东西让你开始:

const char *FindSubString( const char *sub, const char *src )
{
  // Loop through the source string
  while(*src++)
  {
    // Only check for matching character in beginning of sub sequence
    if(*src == *sub)
    {
      return src;
    }
  }
}

This just finds a pointer to the first matching character in a sub-sequence and a source string. I'm sure you can get together something in the inner-loop that checks to see if the sub string matches the next few characters in the src string.

这只是找到指向子序列中的第一个匹配字符和源字符串的指针。我确信你可以在内循环中聚集一些东西来检查子字符串是否匹配src字符串中的下几个字符。

#3


1  

This is a brute force method. just suitable for a quick fix.

这是一种蛮力方法。只适合快速修复。

#include <stdlib.h>
#include <ctype.h>

void extract_2_numbers (char *str, int *a, int *b) {
  *a = atoi (str);
  while (isdigit(*str)) str++;
  while (!isdigit(*str)) str++;
  *b = atoi (str);
}

int main () {
  int x, y;
  char *string = "12--->24";

  extract_2_numbers (string, &x, &y);
  printf ("%d\t%d\n", x, y);
  return 0;
}

Prints this output :

打印此输出:

12    24