hi all i am new to the programing world & specially C with no sources of help available
i hope someone here would help
i am writing a simple code to extract words from a string & printing it
i have spent a lot of time trying to do so, yet i still have problems
not to waist ur time this is the program requirement i am suppose to do
start programming by developing and testing a simple C function. Specifically, write the function
void getCmd (int *argc, char *argv[])
which reads a command line from the standard input device (keyboard) and returns the
number of words/tokens in that command line in the integer variable argc and pointers
to the individual words/tokens in the character vector argv[]. Thus, argc should
contain the number of strings in the command line (the program name and the
arguments) and the array of character strings argv contains each of these strings —
pointer to the program name in argv[0], pointer to the first parameter in argv[1],
pointer to the second parameter in argv[2], etc. You should identify the words/tokens
in a command line by searching for the space character(s) delimiting the words/tokens.
In particular, you should not use any library function to extract the words/tokens in a
character string.
In order to systematically develop your program, first write the algorithm to perform
the above in pseudo code form. Be sure to show the words in a command line are
identified and how argc and argv[] are computed. Then implement your algorithm as
the C function getCmd.
Write a main program (and store it in the file getCmd.c) which calls your getCmd
function to read a command line and print the command line, the value of argc and the
words in the command line as shown below. For example, for the command line
copy ~/Pract1/getCmd.c ~/Pract2/getCmd.c
the output of your main program should be
The command line ‘copy ~/Pract1/getCmd.c ~/Pract2/getCmd.c’ contains the following 3 words:
copy, ~/Pract1/getCmd.c, ~/Pract2/getCmd.c
Test your program with at least the following command lines.
cd ~/Pract2
copy ~/Pract/getCmd.c ~/Pract/getCmd.c
copy copy
cp getCmd.c getCmd.c
del copy
del getCmd.c
ren copy copy.test
ren getCmd.c getCmd.c
This is the code i wrote so far it is a bit stupid but this is really my first shot @ C
and what makes it worse is that i am not suppose to use any of the special library functions ;(.
Code:#include <stdio.h> void getCmd (int *argc, char **argv[]){ int endv; char pos[7]; int p; int tokens; p=0; for (int l=0; *argv[l] != '\r';l++){ if ( argv[l]=='\0'){ p=p+1; pos[p]=l; tokens= tokens+1; } } endv = l; argc = tokens; int loc; loc=0; (*argc) = 0; argv[(*argc)++] = userInput+(pos[loc]); loc++; argv[(*argc)++] = userInput+(pos[loc]); loc++; argv[(*argc)++] = userInput+(pos[loc]); } int main(){ char userInput[256]; printf("type a string: "); fgets(userInput, 256, stdin); printf("The command line ' %s ' contains the following ", userInput); int i; int cmdc; char* cmdv[20]; getCmd(&cmdc, cmdv); for(i=0;i<cmdc; i++){ printf("argument %d is %s\n", i, cmdv[i]); } return 0; }