Question

i would remove the "-" from the ISBN String. But my code dont print me the value out. Where is the fault?

char *ISBN[20];  //example: 3-423-62167-2
*p = ISBN;

strcpy(ISBN, ptr); //Copy from a Buffer
printf("\nISBN Array: %s", ISBN); //This works!

while(*p)
{
    if (isdigit(*p))
    {
        long val = strtol(p, &p, 10);
        printf("%ld\n", val);         //Do not show anything!
    }
    else
    {
        p++;
    }
}
Was it helpful?

Solution 3

Incorrectly using strtol is unnecessary; the 2nd argument is not an input, but an output i.e. it sets it to the last interpreted character. Above all, why do you want to convert the character to a long and then convert it back again to a character, when all you need is a character to print?

char ISBN[] = "3-423-62167-2";
char *p = ISBN;
while (*p)
{
    if (isdigit(*p))
        printf("%c", *p);
    ++p;
}

EDIT:

To make the whole string into a long:

unsigned long long num = 0;
while (*p)
{
    if (isdigit(*p))
    {
        const char digit = *p - '0';
        num = (num * 10) + digit;
    }
    ++p;
}

OTHER TIPS

What about:

for (char* p = ISBN; *p != '\0'; p++)
{
    if (isdigit(*p))
    {
        printf("%c", *p);
    }
}

If you want a long: Save the characters in a char[] (instead of printf()) and then, when done, convert that to a long. You could even use your ISBN array to do an in-place convert:

int i = 0;
for (char* p = ISBN; *p != '\0'; p++)
{
    if (isdigit(*p))
    {
        ISBN[i++] = *p;
    }
}

ISBN[i] = '\0';

long isbn = strtol(ISBN, NULL, 10);

BTW, you forgot p++ when is digit() is true.

The following code works for me:

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

int main()
{

char *ptr = "3-423-62167-2";
char ISBN[20];  // should be either ISBN[] or char *ISBN for a string
char *p = ISBN; // declared this one here.. else should be p = ISBN

strcpy(ISBN, ptr);
printf("\nISBN Array: %s\n", ISBN);

while(*p)
{
    if (isdigit(*p))
    {
        long val = strtol(p, &p, 10);
        printf("%ld\n", val);
    }
    else
    {
        p++;
    }
}

}

Have marked the corrections in the comments!

Assuming p is char * pointer you should update your code to

//-----v no *
  char ISBN[20];  //example: 3-423-62167-2
  p = ISBN;
//^-- no *

keep rest of the code as is.

I think what the OP wants is to convert a string with hyphens to a long integer. This function converts the decimal digits of a string to a long. Hyphens (in any place) are ignored, other characters, including space, lead to a reading error:

/*
 *      Return ISBN as long or -1L on format error
 */
long isbn(const char *str)
{
    long n = 0L;

    if (*str == '\0') return -1L;

    while (*str) {
        if (isdigit(*str)) {
            n = n * 10 + *str - '0';
        } else {
            if (*str != '-') return -1L;
        }
        str++;
    }

    return n;
}

Note that a long has the same size as an int on some machines and may not be wide enough to store a numeric ISBN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top