-2

I was wondering how I can copy a part of a string to another one, so I checked this web to see if I could get an answer, and I did: How can I copy part of another string in C, given a starting and ending index

However, when I use this

strncpy(dest, src + beginIndex, endIndex - beginIndex);

on my code, it does not work. Why? I think I have added the indexs properly.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAX 15

int main()
{
    int j=0;
    int i;
    char RPNarray[MAX];
    printf("Enter the RPN");
    fgets(RPNarray,MAX,stdin);
    int t=strlen(RPNarray);
    double RPN[t][2];

    for(i=0;i<=t;i++)
    {
        if(isspace(RPNarray[i]))
        {
            i=i+1;
        }
        else if (isdigit(RPNarray[i]))
        {
            int q,h;
            q=i;
            for (h=i;isdigit(RPNarray[h]);h++)
            {
                h=h+1;
            }
            int r;
            double u;
            r=h-q;
            char number[r];
            printf("%s ", RPNarray);
            strncpy(number,RPNarray + q,h - q);
            printf ("%s\n",number);
            u=atof(number);
            printf("%lf.\n",u);
            RPN[j][0]=0;
            RPN[j][1]=u;
            i=i+1;
            j=j+1;
        }
    }
    int b;
    for (b=0;b<=j;b++)
    {
        printf("%lf %lf \n",RPN[b][0],RPN[b][1]);
    }
}

r is the number of digits of the number, q the position of the first digit of the number and h the first position after the number that is not a number.

This program should print the RPN matrix as the code says.

 (0,(first number we introduce via stdin)
 (0,(second number we introduce via stdin)
 (0,(third number we introduce via stdin)

I have also added more prints to see where is the problem, and as you can see, the second print is not printed as it should.

If the input is:

56 6 
The output is:
56 6
 56OT?
56.000000.
vector 0.000000 56.000000 
vector 0.000000 0.000000

What can I do so the output is:

56 6
 56
56.000000.
56 6
 6
6.000000.
vector 0.000000 56.000000 
vector 0.000000 6.000000

Thank you

Community
  • 1
  • 1
Jokeiis
  • 11
  • 2
    Please edit your post and fix the indention. – Lundin Jun 03 '16 at 13:03
  • Learn how to use a debugger, and step through the code, line by line. – Some programmer dude Jun 03 '16 at 13:08
  • The loop ending conditions `<=` are suspicious. In the first one `t` is going past the length of the string. In the second one `b` is indexing an element of `RPN[b]` that has not been set at the current value of `j`,which was incremented after it was used. – Weather Vane Jun 03 '16 at 13:24
  • for ease of readability and understanding: 1) consistently indent the code 2) variable names should indicate content or usage (or better, both) 3) separate code blocks (for, if, else, while, do...while, switch, case, default) via a blank line. – user3629249 Jun 03 '16 at 14:34
  • do not `#include` header files those content is not used. I.E. do not include math.h – user3629249 Jun 03 '16 at 14:36
  • when writing code, use appropriate comments. Comments should explain what the code is trying to do, or what has been accomplished or explain an algorithm, not trivialities within the code – user3629249 Jun 03 '16 at 14:42
  • the call to `fgets()` will also input the trailing newline ('\n'). Usually the code should eliminate any trailing newline. Note: the returned value from `fgets()` should be check (!=NULL) to assure the operation was successful – user3629249 Jun 03 '16 at 14:44
  • the function: `strlen()` returns a `size_t`, not an `int` – user3629249 Jun 03 '16 at 14:45
  • all the code seems to be trying to do is extract the first numeric sub string from the input and convert that to a integer. then place that integer into the `RPN[][]` array, which the code is using a very convoluted algorithm to perform. However, the code does not properly handle a input with no embedded numeric string nor input with any alphabetic characters. the posted code fails if the numeric sub string contains a decimal point `.` Note: no need to extract the sub string, the `atof()` function does not care where the string is located – user3629249 Jun 03 '16 at 14:51

1 Answers1

0

Maybe you just needed to continue instead of incrementing?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

#define MAX 15

int main() {

    int j = 0;
    int i;
    char RPNarray[MAX];
    printf("Enter the RPN");
    fgets(RPNarray, MAX, stdin);
    int t = strlen(RPNarray);
    double RPN[t][2];

    for (i = 0; i <= t; i++) {
        if (isspace(RPNarray[i])) {
            continue;//i = i + 1;
        }
        else if (isdigit(RPNarray[i])) {
            int q, h;
            q = i;
            for (h = i; isdigit(RPNarray[h]); h++) {
                h = h + 1;
            }
            int r;
            double u;
            r = h - q;
            char number[r];
            printf("%s ", RPNarray);
            strncpy(number, RPNarray + q, h - q);
            printf("%s\n", number);
            u = atof(number);
            printf("%lf.\n", u);
            RPN[j][0] = 0;
            RPN[j][1] = u;
            i = i + 1;
            j = j + 1;
        }
    }
    int b;
    for (b = 0; b <= j; b++) {
        printf("%lf %lf \n", RPN[b][0], RPN[b][1]);
    }
}

Test

Debug/gnu
Enter the RPN56 6
56 6
 56
56.000000.
56 6
 6

6.000000.
0.000000 56.000000 
0.000000 6.000000 
0.000000 0.000000 

It looks like the output you want. All I did was to change one statement.

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424