how to split command line arguments equally in C -
i writing c program using command line arguments. when ever give string in command line space splits sub files ,
for ex- sample output
$ ./cmdline 4646 313 256 96664 commandline args count=5 exe name=./cmdline split_part1=4646 split_part2=313 split_part3=256 split_part4=96664
i want same output without giving space in between string. how do,each sub part should split equal number of strings . please help.
#include <stdio.h> int main (int argc, char *argv[] ) { int i=0; printf("\n commandline args count=%d", argc); printf("\n exe name=%s", argv[0]); (i=1; i< argc; i++) { printf("\n"); printf("\n split_part%d=%s", i, argv[i]); } printf("\n"); return 0; }
following comments, simple algorithm achieve want looks like
(for fixed token size of 5
, can modified per requirement later)
- check if
argc ==2
. - calculate string length of
argv[1]
, store it. - take variable used index.
- write loop breaking condition
index < string_length
, increment conditionindex +=5
. use
printf()
(used illustration, can add storing part also) likeprintf("%5s\n" &(argv[i][index]));
Comments
Post a Comment