Repeat for part of string

This is a discussion on Repeat for part of string within the C Programming forums, part of the General Programming Boards category; I have my program working so far to read in a string of the form ax^n, and evaluate ax^n for ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    Repeat for part of string

    I have my program working so far to read in a string of the form ax^n, and evaluate ax^n for an inputted x value. Now what I am trying to is to extend this to an nth order polynomial:

    a[n]x^n + a[n-1]x^(n-1) + ... + a[2]x^2 + a[1]x + d

    I have been racking my brain, and cannot think of any way to do it. What I need to do is to repeat the whole process for the next term in the polynomial, until the end of the inputted string is reached (polynomial[i]=='\n'), adding the value of each term as I go.

    To me, a for loop seems like an obvious choice, since I can use this to sum the terms and terminate at the end of the string, but I can't see a way to repeat my process for the next term (so basically processing the string chunk by chunk).

    The problem is that each term may have a different length, so I can't increment the loop by a fixed amount, but I can return the length of the given term as part of my 'process.' I can't explain this too well, but this might make it clearer:

    -calculate term 1 (and return length of term) (I have this working already)
    -move to term 2
    -calculate term 2 (and return length of term)
    -move to term 3
    -etc....
    -stop when polynomial[i]=='\n' (end of string)
    -Add all terms

    I there any way to do this? It would be useful If I could shift the contents of the string n places to the left (where n is the length of the given term), such that polynomial[n] is set to polynomial[0] before the next repeat.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If string is your array of chars, then string+6 is the string, but without the first six characters.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Do you mean string[i+6]? If so I've used that before, and don't think it coud really help me here. I've thought of a way to do what I want, but it leads to another question - Can you have a parametric function, that takes a single value for it's input and gives two or more outputs (for example it might output a char and two floats as three independent variables), and then when you call the function, select which output you want to use? For instance when you call such a function, something like:

    Code:
    int a = b*function(c);
    printf("a=%d",a);
    The ouput will be a=nan, since it doesn't know which output to use and it could not make sense to do the above.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No I don't mean string[i+6]. I mean string+6. Watch:
    Code:
    #include <stdio.h>
    
    int main(void) {
        char test[] = "Skipping characters for an example.";
        printf("%s\n", test);
        printf("%s\n", test+10);
        return 0;
    }
    As to the function, the closest thing to what you specify would be to return a struct. I won't say whether that's the closest thing to what you want.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Ah, I see thanks, thats what I'm looking for. As for the function, I've found a way to do that as well, but I have yet another question ...when you use a string as a parameter of a function, do you just use

    Code:
     function(char string[]) {...}
    , (without specifying the length of the string), and then use

    Code:
    function(string)
    to call it? Or do you have to put square brackets and/or specify string length as well?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That would be correct.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    yay!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21