#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Germany
    Posts
    4
    Rep Power
    0

    Question how to separate Int in String varaible


    Hi Everybody
    I habe a small Problem.
    In my Code i became the following variable
    xmlChar *fitness;
    fitness = xmlstrdup(xmlLDAPGetValue(cxt,result));
    with the Value ie (alpha;beta) where alpha and beta are int or double Value.
    How can i separate them.
    I want something like
    fitness[1] = alpha;
    fitness[2] = beta;
    We don't have to forget that the String ist "(alpha;beta)"
    thanks
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,640
    Rep Power
    4247
    One way to do it using strstr() and strcpy().
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void) {
      char string[] = "(14;22.087)";
      char *ptr, *ptr2;
      char temp[10], temp2[10];
      int i; double d;
    
      /* Find out the terminator positions */
      ptr = strstr(string, ";");
      ptr2 = strstr(ptr, ")");
      /* Put null terminators temporarily */
      *ptr = '\0'; *ptr2 = '\0';
    
      /* Now copy the strings to temp buffers */
      strcpy(temp, &string[1]);
      strcpy(temp2, ptr + 1);
    
      /* undo the null terminators */
      *ptr = ';'; *ptr2 = ')';
    
      /* Convert the two strings to the respective types */
      i = atoi(temp);
      d = atof(temp2);
    
      printf("%d\n%g\n", i, d);
    
      return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Germany
    Posts
    4
    Rep Power
    0
    Thanks,it's working.
    Something else
    if i want to input the string[] and ask in the Code wather it ist Null or not how should i do it.
    i.e
    scanf("%d%g\n", &i,&d);
    if (NULL == string[]) //Is it correct???
    return(1.0);
    else{
    printf("%g\n%d\n", i, d);}

IMN logo majestic logo threadwatch logo seochat tools logo