Thread: String Prefix Validation

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    String Prefix Validation

    Hello cboard! I'm both new to the forum, and new to C Programming and am struggling to achieve validation of the start of a string passed through terminal.


    Below is both the code and my general thinking slung together:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(int argc, char *argv[]) {
    
    
        char *address;
        char *addresspre;
    
    
        //request input
        printf("Enter a URL (http://example.com):\n");
    
    
        //address is passed as argv[1] when run from terminal
        printf("Saving address \"%s\".\n", argv[1]);
    
    
        //copies argv[1] into 'address'
        strcpy(address, argv[1]);
        
        addresspre = address;
        address = address+7;
    
    
        strncmp(address, addresspre, strlen(addresspre));
    
    
    }
    Here is what I'd type to run from terminal:

    Code:
    ./httptest http://blah.com
    (or something to that effect)

    I appreciate it doesn't achieve much but I would like this part of my program to check that 'argv[1]' (as passed through terminal on execution) starts with 'http://'. It's all part of a bigger program I am working on; here I have just taken out what I am struggling with.

    Ideally I'd like an if statement to be satisfied with 'http://' being present and continue - with a block of working code I have all ready got - and an else to printf a failure message and return 0 (the latter I can do myself).

    Apologies for for slinging in random working parts but I am new to C and have only past experience with Java, a whole different kettle of fish as I'm sure you'll know.

    I'd much appreciate any help or direction with getting further forward with this. Thanking you in advance.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to allocate space for address. Something like
    Code:
    char address[200];
    I have no idea what you're trying to do with addresspre.
    Try something like
    Code:
    strncmp(address, "http://", 7)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Something like this could work.

    Code:
        if(strncmp(argv[1], "http://", 7) != 0) {
            printf("start of input must be: http://\nyou wrote: %s\n", argv[1]);
            return -1;
        }
    Preferably do this at the beginning of main, right after you checked argc, no point in going further if the input is not correct.
    Last edited by Subsonics; 03-05-2012 at 11:14 AM.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Thank you oogabooga, and Subsonics.

    Though the first response suits the code I put in the first post, Subsonics' solution fits nicely into the program I'm working on has a whole, (which isn't the same as here).

    Anyhow, I got the validation to work nicely so thank you very much guys!

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Resilius View Post
    Thank you oogabooga, and Subsonics.

    Though the first response suits the code I put in the first post, Subsonics' solution fits nicely into the program I'm working on has a whole, (which isn't the same as here).

    Anyhow, I got the validation to work nicely so thank you very much guys!
    Just, don't forget to check that argc == 2, before you do anything else. If you use argv without any arguments you will segfault. Or more precisely: if argc != 2 bail.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    3
    Quote Originally Posted by Subsonics View Post
    Just, don't forget to check that argc == 2, before you do anything else. If you use argv without any arguments you will segfault. Or more precisely: if argc != 2 bail.
    I've got that check at the start of the html stripper I've written (the main prog).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can the new string literal prefix in C++11 be adaptive?
    By danath in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2011, 03:22 AM
  2. Validation of string format
    By Evenstevens in forum C Programming
    Replies: 8
    Last Post: 05-03-2009, 05:04 AM
  3. string validation
    By sql.scripter in forum C++ Programming
    Replies: 31
    Last Post: 05-24-2005, 03:11 PM
  4. String Validation
    By duvernais28 in forum C Programming
    Replies: 15
    Last Post: 01-30-2005, 08:11 PM
  5. String Validation (Newbie)
    By colinuk in forum C Programming
    Replies: 6
    Last Post: 01-26-2005, 08:22 AM