-1

I have the following code:

  host = argv[1];

  if ((argv[1]) = "http://"); {{
    host = host + 7;
  }

the host is a url which is entered by argument it can be either http://google.com or www.google.com

so I want to check if the arguments contains "http://" and remove it by removing the first 7 characters is there such a function in c?

Angel
  • 1
  • 3
    Comparing char pointers won't work. Use `strcmp()` (or `strncmp()` if you only want to compare the first 7 characters.) – r3mainer Nov 21 '16 at 23:15
  • Possible duplicate of [How do I properly compare strings in C?](http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – r3mainer Nov 21 '16 at 23:17

1 Answers1

0

Try this:

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv){
  char *host = argv[1];
  if (strncmp(argv[1], "http://", strlen(argv[1])) == 0) {
      host = host + 7;
  }
  printf("%s\n", host);
}
  • Thank you for the fast reply but I need to use the host variable as this is part of a long program so all I want to do is to check if there is "http://" in the argument and cut it my way host = host + 7; The http:// is always start of the argument so these are the first 7 characters for sure. – Angel Nov 21 '16 at 23:26
  • I see! Yeah, your way accomplishes it as well, and is arguably cleaner than mine(I like explicit pointer arithmetic). strstr() is what you're looking for! – Martin Jungblut Schreiner Nov 21 '16 at 23:29
  • There, edited the answer to reflect your code, the way you want to use it. – Martin Jungblut Schreiner Nov 21 '16 at 23:37
  • Actually, strncmp is better in this case, as @squeamish-ossifrage said, since you only want to compare a limited number of characters from the strings. – Martin Jungblut Schreiner Nov 21 '16 at 23:41
  • I have done it here is the working code: if ((strncmp(argv[1], "http://", 7)) == 0) { host = host + 7; } Thank you for your help! – Angel Nov 22 '16 at 10:19