Click here to Skip to main content
15,884,009 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm to modify the code below by turning char** end in findInteger into int* end

int findInteger(const char str[81], char** end) {
    int i = (int)(*end - str), j = 0;
    bool isNum = false;
    char str1[40];
    char prev = ' '; //instead of str[0], if number at beginning
    while (str[i]) {
        if (isdigit(str[i])) {
            if (!isNum && (prev == ' ' || prev == '\t' || prev == ',' || prev == '.' || prev == '!' || prev == '\0')) isNum = true;
            if (isNum) {
                str1[j] = str[i];
                j += 1;
            }
        }
        else {
            if (isNum) {
                if (str[i] == ' ' || str[i] == '\t' || str[i] == ',' || str[i] == '.' || str[i] == '!' || str[i] == '\0') {
                    str1[j] = 0;
                    *end = (char*)&(str[i]);
                    return atoi(str1);
                }
                else {
                    isNum = false; j = 0;
                }
            }
        }
        prev = str[i];
        i += 1;
    }
    if (isNum) {
        str1[j] = 0;
        *end = (char*)&(str[i]);
        return atoi(str1);
    }
    *end = (char*)&(str[i]);
    return NULL;
}

int getNumbers(const char str[81], int numbers[40]) {
    char* end = (char*)str;
    int c = 0, num;
    while (*end) {
        if ((num = findInteger(str, &end)) != NULL) {
            numbers[c] = num;
            c += 1;
        }
    }
    return c;
    //return 0;
}

int main() {
    char string[20][81];
    int string_count;

    input_printf("Input number of lines (integer in the range [1,20]): \n");
    scanf("%d", &string_count);
    scanf("%*1[\n]");


    if (string_count < 1 || string_count > 20)
    {
        error_printf("invalid input data");
        return 0;
    }

    input_printf("Input text: \n");
    for (int i = 0; i < string_count; i++)
    {
        gets_s(string[i]);
    }

    for (int i = 0; i < string_count; i++) {
        int num[40];
        int length1 = getNumbers(string[i], num);

        if (length1 == 0) {
            error_printf("no solution");
        }

        else {
            //printf("%d\n", length1);
            for (int j = 0; j < length1; j++) printf("%d  ", num[j]);
            printf("\n");
        }

        return 0;
    }
}


What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "str_unit.h"
#include "testing.h"
#pragma warning(disable : 4996)

int findInteger(const char str[81], int* end) {
    int i = strlen(str), j = 0;
    bool isNum = false;
    char str1[40];
    char prev = ' '; //instead of str[0], if number at beginning
    while (str[i]) {
        if (isdigit(str[i])) {
            if (!isNum && (prev == ' ' || prev == '\t' || prev == ',' || prev == '.' || prev == '!' || prev == '\0')) isNum = true;
            if (isNum) {
                str1[j] = str[i];
                j += 1;
            }
        }
        else {
            if (isNum) {
                if (str[i] == ' ' || str[i] == '\t' || str[i] == ',' || str[i] == '.' || str[i] == '!' || str[i] == '\0') {
                    str1[j] = 0;
                    end = (int*)&(str[i]);
                    //return atoi(str1);
                }
                else {
                    isNum = false; j = 0;
                }
            }
        }
        prev = str[i];
        i += 1;
    }
    if (isNum) {
        str1[j] = 0;
        end = (int*)&(str[i]);
        //return atoi(str1);
    }
    end = (int*)&(str[i]);

}

int getNumbers(const char str[81], int numbers[40]) {

    int end = atoi(str);
    int c = 0, num;
    while (end) {
        if ((num = findInteger(str, &end)) != NULL) {
            numbers[c] = num;
            c += 1;
        }
    }
    return c;
}

int main() {
    char string[20][81];
    int string_count;

    input_printf("Input number of lines (integer in the range [1,20]): \n");
    scanf("%d", &string_count);
    scanf("%*1[\n]");


    if (string_count < 1 || string_count > 20)
    {
        error_printf("invalid input data");
        return 0;
    }

    input_printf("Input text: \n");
    for (int i = 0; i < string_count; i++)
    {
        gets_s(string[i]);
    }

    for (int i = 0; i < string_count; i++) {
        int num[40];
        int length1 = getNumbers(string[i], num);

        if (length1 == 0) {
            error_printf("no solution");
        }

        else {
            //printf("%d\n", length1);
            for (int j = 0; j < length1; j++) printf("%d  ", num[j]);
            printf("\n");
        }

        return 0;
    }
}


But it still doesn't work, and the main problem is coming from the getInteger function
Posted
Updated 8-Jul-20 4:44am
Comments
Garth J Lancaster 3-Jul-20 9:44am    
"But it still doesn't work" - maybe you'd like to use 'Improve question' and show/state why it's not working or what you get for various inputs - we cant read your screen or your mind

The first thing you need to learn is that int*and char* are not the same: you cannot assume that a valid char pointer will give a valid int pointer - because int pointers must be on 2, 4, or 8 byte boundaries (depending on the size of an int in your system) while char pointers are single byte boundary. So an "odd" value in a char pointer will not reference the same memory location when treated as a int pointer - some of the least significant bits will be discarded or ignored. You need to be aware of that if you are changing the calling code as well.

Other than that,
Quote:
it still doesn't work

tells us nothing: we know there is a problem or you wouldn't be posting here.
Quote:
the main problem is coming from the getInteger function

Also tells us nothing because there is no such function in your code!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
KarstenK 3-Jul-20 10:57am    
Arent all pointers the same size depending on compiler settings? (But the memory to which they point should be different)
When option available to you is to create a union of pointers.

A simple example:
C++
union myPointers {
  char * c;
  int  * i;
}

Now, all you need to do is set the value of the pointer to the (first) character to
the 'c' member in the struture and then read it back via the 'i' member.

Something similar to:
C++
union myPointers x;
x->c = s;  // s is the address of the your char string
           // *x->i contains an array of int's  addressible as an
           // indexed array if you like


You can add a other types to this sort of union.

It can be done other ways C - Unions - Tutorialspoint[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900