# Repeat for part of string

• 10-25-2008
bertazoid
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)

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.
• 10-25-2008
tabstop
If string is your array of chars, then string+6 is the string, but without the first six characters.
• 10-26-2008
bertazoid
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.
• 10-26-2008
tabstop
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.
• 10-26-2008
bertazoid
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?
• 10-26-2008
tabstop
That would be correct.
• 10-26-2008
bertazoid
yay! :p