当前位置:网站首页>Recursion will make strtok more attractive

Recursion will make strtok more attractive

2022-06-27 01:23:00 Embedded Linux,

The string inversion problem issued a few days ago , Later, a new student used the recursive method to realize , Looked at the , It's really beautiful .

The previous question of string inversion

The code is as follows

#include "stdio.h"
#include "string.h"
char input[] = {"the sky is blue cris 1212321 apple"};

void reverse(char *s,char *delim) { 
    char* temp = strtok(s,delim);
    if(temp != NULL) {
        reverse(NULL,delim);
        printf("%s ",temp); 
    }
}


int main() { 
 printf("%s\n",input); 
 reverse(input," ");
 return 0;
}

strtok Is a string splitting function , But many people don't know how to use this function .

strtok Function interpretation

char *strtok(char s[], const char *delim)

Put the input string s Decompose into a set of strings .

delim Is a split string , Notice here is the string .

First call ,s Point to the string to be decomposed , Then call again to put s set NULL.

for example :

#include "stdio.h"
#include "string.h"
char input[] = {"the sky is blue cris 1212321 apple"};

int main() {
 char *temp = NULL; 
 printf("%s\n",input); 
 temp = strtok(input," ");
 printf("input=%s,temp=%s\n", input, temp);
 temp = strtok(NULL," ");
 printf("input=%s,temp=%s\n", input, temp);
 return 0;
}
372d40f1c2cb5d94894a6393297e47c6.png

The header file

#include<string.h>

Detailed instructions for use

  • Start

strtok Find the string that needs to be split delim after , Will take this. delim Set to \0 , This can also be seen from the above example , After segmentation , We'll use printf The output will stop at the first split string position .

  • end

When the string finds the end , Function will return NULL, So we can use NULL To determine whether the function execution ends .

  • Be careful

This function will destroy the contents of the original string , After the first segmentation , Original string s It's the first string after the segmentation .

strtok Function source code

char *
strtok_apple(
 register char *s,
 register const char *delim)
{
 register char *spanp;
 register int c, sc;
 char *tok;
 static char *last;


 if (s == NULL && (s = last) == NULL)
  return (NULL);

 /*
  * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
  */
cont:
 c = *s++;
 for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
  if (c == sc)
   goto cont;
 }

 if (c == 0) {  /* no non-delimiter characters */
  last = NULL;
  return (NULL);
 }
 tok = s - 1;

 /*
  * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  * Note that delim must have one NUL; we stop if we see that, too.
  */
 for (;;) {
  c = *s++;
  spanp = (char *)delim;
  do {
   if ((sc = *spanp++) == c) {
    if (c == 0)
     s = NULL;
    else
     s[-1] = 0;
    last = s;
    return (tok);
   }
  } while (sc != 0);
 }
 /* NOTREACHED */
}

Explain the code above

#include "stdio.h"
#include "string.h"
char input[] = {"the sky is blue cris 1212321 apple"};

void reverse(char *s,char *delim) { 
    char* temp = strtok(s,delim);
    if(temp != NULL) {
        reverse(NULL,delim);
        printf("%s ",temp);// At the first call, the final output will be , When the last call is not equal to null, the first output is  
    }
}


int main() { 
 printf("%s\n",input); 
 reverse(input," ");
 return 0;
}

If recursive understanding is not very clear , It can be used for Let's output

#include "stdio.h"
#include "string.h"
char input[] = {"the sky is blue cris 1212321 apple"};

int main() {
 char * output[1024]; 
 int len = 0; 
 
 printf("%s\n",input);
 
 char* temp = strtok(input," "); 
 
 for (len=0;temp!=NULL;temp = strtok(NULL," "),++len) {
  output[len] = temp;
 }
 
 for(;len--;) {
  printf("%s ", output[len]);
 }
 return 0;
}
1e3fafe1d75fe753815b52278b256386.png

3dd1809b725e5684d6cfde13ee9bc856.gif

原网站

版权声明
本文为[Embedded Linux,]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270049078581.html