The C language divides strings by spaces and generates multiple string arrays

Source: Internet
Author: User

The science of a little C language has long been forgotten, but do the computer system work to write with C, deeply c memory allocation and string processing too much trouble.
The job needs to cut the string (command) received after the program is run into multiple character arrays (instructions and parameters), and I can't find it on the Internet, and I write a (I don't know if C itself has this function, it can't be found).
Because it is too weak, do not know how to automatically allocate memory, so I set: instruction up to 10, the length of each instruction 50.
The function returns a pointer to a two-dimensional array of characters.
Char**trim (Char*cmd) {  Char**argv; Char**RTR; argv= (Char* *) malloc (Ten*sizeof(Char*));  for(inti =0; I <Ten; i++) Argv[i]= (Char*) malloc (sizeof(Char)* -); RTR=argv; inti =0;  while(*cmd! =' /'&& *cmd! ='\ n') {    if(*cmd! =' ') {      * (*argv+i) = *cmd; ++cmd; I++; }    Else{i=0; ++cmd; ++argv;  while(*cmd = =' ') {    ++cmd; }    }  }  returnRTR;}

The C language divides strings by spaces and generates multiple string arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.