Can anyone do this??

As you now know, the UNIX (and MS-DOS) command line arguments are available through the main function's two arguments argc and argv. The integer argc is the argument count, and the array argv is an array of character pointers, each element being a pointer to the NUL-terminated character string representing the corresponding argument.

Write a main function which prints out the number of command-line arguments presented and which then prints out each argument on a separate line.

For example, if your program is called 'foo' and the command line comprises:

./foo here there 21

the result should be:

4
./foo
here
there
21


Comments

  • Sure we can all do that, but if we did it, it would not teach you a thing. Give it a whirl and if you have problems as a specific question.
  • : Sure we can all do that, but if we did it, it would not teach you a thing. Give it a whirl and if you have problems as a specific question.
    :
    I agree with above mesg.
    If we do it how will you learn.
    I'l give you a clue
    take command line arguments into main().
    Refer how to take "command line arguments as i/p".
    if you don't get it at all then we will help.

  • #include

    int main(int argc, char *argv[]){
    std::cout<< argc<<"
    ";

    for(int i=0;i<argc;i++){
    std::cout<< argv[i]<<"
    ";
    }
    return 0;
    }
    But ive a new one now for you:
    Write a program to read a single parameter from the command line and to check whether or not the parameter is a palindrome.

    A palindrome is a word or phrase where the sequence of characters is the same whether read left to right or right to left. For example, the word Navan is a palindrome. The phrase Able was I ere I saw Elba is also a palindrome.

    To make things a bit simpler, eliminate the problem of dealing with uppercase and lowercase letters by typing everything in just one case.
    Im not too sure about this one all i know is that you want to compare the left with the right,i can write a program for a five letter palindrome:
    #include <iostream>

    int main(int argc,char*argv[]){
    if(argv[1][0]==argv[1][4]){
    std::cout<<"First character is equal to fifth character
    ";
    }
    else{
    if(argv[1][0]!=argv[1][4]){
    std::cout<<"It ain't a palindrome
    ";
    return 0;
    }
    }
    if(argv[1][1]==argv[1][3]){
    std::cout<<"Second character is equal to fourth character
    ";
    }
    else{
    if(argv[1][1]!=argv[1][3]){
    std::cout<<"It ain't a palindrome
    ";
    }
    }
    if(argv[1][2]==argv[1][2]){
    std::cout<<"Third character is equal to third character
    ";
    std::cout<<"Therefore if left is equal right and middle is equal to middle its a palindrome
    ";
    }
    }
    return 0;
    }
  • I think what I would do is make another copy of the string but insert the characters in reverse order, then compare the two strings.

    [code]
    int main(int argc, char**argv)
    {
    char dup[255];
    // duplicate the string, but in reverse order
    char* ptr1 = dup;
    char* ptr2 = argv[1] + strlen(argv[1]) - 1;
    while(ptr2 >= argv[1])
    *ptr1++ = *ptr2--;
    *ptr1 = 0;
    // compare case-insensitive
    int cmp = stricmp(argv[1],dup);
    // display the results
    cout << "argv[1]: " << argv[1] << " dup: " << dup
    << " cmp: " << cmp << endl;
    cin.ignore();
    return 0;

    }
    [/code]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories