Author Topic: [C]Question,Comparing a string to an array full of strings.  (Read 1590 times)

0 Members and 1 Guest are viewing this topic.

Offline Rafy

  • Peasant
  • *
  • Posts: 111
  • Cookies: 5
    • View Profile
[C]Question,Comparing a string to an array full of strings.
« on: January 04, 2012, 02:02:07 pm »
So as you can see , what I want to do is compare a string named "in" to an array of strings named "arr". Here is the code I have so far.
Code: [Select]

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


int main()
{
 
 char arr[3][128]={"chk","help","ext"};
 char in[128];
 int cm=1;
 int i;
 while(cm!=0)
 {
  printf(">Type a command and press return.\n");
  printf(">> ");
  scanf("%s",in);
  for(i=0;i<3;i++)
  {
    cm=strmcp(in,arr[i]);
  }
 }
 printf("%d\n", cm);
return 0;


}

The objective is to find the string inside the array,which in this case it's a command.If you know a better way to accomplish it instead of this one please mention it.
I know the compare part is totally wrong but that's what I came up with.I don't know if it's any help but I am doing everything in the Macintosh terminal using Nano and gcc.any help is appreciated.The program is text based , I dont know anything about GUI yet regarding C.


PS: almost forgot , I need another thing. How can I check using C if a website is online?I tried to find something on the net regarding ping.So far the only solution I have managed to come up with is to execute the native ping command of the mac/linux terminal inside my tool.
« Last Edit: January 04, 2012, 02:04:03 pm by Rafy »
If it moves shoot it,if it runs... hack it!

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: [C]Question,Comparing a string to an array full of strings.
« Reply #1 on: January 04, 2012, 02:31:31 pm »
Code: [Select]
#include <stdio.h>
#include <string.h>


int main()
{
 
 char arr[3][128]={"chk","help","ext"};
 char in[128];
 int i=0;
 do
 {
  printf(">Type a command and press return.\n");
  printf(">> ");
  scanf("%s",in);
  while( (i++ < 3) && (cm!=0 ) ) cm=strcmp(arr[1], in)!=0)
 }
  while(cm!=0);
 printf("%d\n", cm);
return 0;
}

Also, scanf is really insecure. No input lenght control.
About checking a webpage, look for sockets. Something like (not finished, more than probably not working, on the fly):
Code: [Select]
int isOnline(char *web, int port)
{
  int sock=socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in addr;
  struct hostent *host;
  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_family=AF_INET;
  addr.sin_port=htons(port);
  addr.sin_addr = *((struct in_addr *) host->h_addr);
  if(connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr)) return 1;
  else return 0;
}

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [C]Question,Comparing a string to an array full of strings.
« Reply #2 on: January 04, 2012, 11:12:28 pm »
Here you go, I think this is a good example :) :

Code: [Select]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NUMOPT 3
#define MAX 6 // 4 + newline + null
#define NONE -1

const char * options[NUMOPT] = {"chk", "help", "exit"};

int get_option(void)
{
    char input[MAX];

    if (!fgets(input, MAX, stdin)){
        fprintf(stderr, "EOF!\n");
        exit(1);
    };
   
    int input_size = strlen(input);

    if (input_size > 1){ // removing newline
        if (input[input_size-1] == '\n')
            input[input_size-1] = '\0';
        else
            while (getchar() != '\n') continue;
    }
   
    int i;
    for (i = 0; i < NUMOPT; i++){
        if (strncmp(options[i], input, input_size) == 0)
            return i;
    }
    return NONE;
}

int main(void)
{
    int selected = NONE; // selected option index
    do {
        printf(">Type a command and press return.\n");
        printf(">> ");
    } while ((selected=get_option()) == NONE);

    printf("You've selected option %s\n", options[selected]);

    return 0;
}

The new C standard should introduce new standard function gets_s which will replace fgets (and of course gets) for getting a line of input from the user without getting the trailing newline.
« Last Edit: January 05, 2012, 06:05:09 am by s3my0n »
Easter egg in all *nix systems: E(){ E|E& };E

Offline Rafy

  • Peasant
  • *
  • Posts: 111
  • Cookies: 5
    • View Profile
Re: [C]Question,Comparing a string to an array full of strings.
« Reply #3 on: January 09, 2012, 02:42:03 pm »
Thanks everybody.About scanf being insecure, that's what they taught us in university and that's what they want to see in the lab exam.Maybe , later they will introduce something new that works better. 
If it moves shoot it,if it runs... hack it!