Determining length needed for string

This is a discussion on Determining length needed for string within the C Programming forums, part of the General Programming Boards category; Ok, my problem is that my expand function prints the expanded version of the shorthand to stdout, and I would ...

Determining length needed for string

Ok, my problem is that my expand function prints the expanded version of the shorthand to stdout, and I would like it to return a pointer to a newly created string containing the expanded version. I have no idea how many notations are going to be provided at runtime, so my question is as follows: how can I determine how much space is exactly needed for the newly expanded string so I can dynamically allocate the space. Ahhh I hope a seasoned vet can help me out, or point out why this is a proposterous idea.

expand()
while( all characters haven't been read ){
if( char is '-' and is either leading or trailing )
print '-'
if ( a valid range is found )
print expanded notation of range
else
if( an invalid range or an invalid char is found )
print error message
if( invalid char )
break while
}

That will just make life more difficult for you. Given the range input, you can determine the length of the range, thus allowing you to dynamically create an array of chars that will store all the characters of the range.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Arrays are much easier to work with, and debug. Linked lists are OK if you really need to conserve memory AND your run-time is not hurt by including them.

So in your code, you might:

1) check the first char, if it's a hyphen put it into the expanded array, exp[]. If it's not a hyphen, begin putting it and all the letters greater than it, until you reach the last letter. Include any hyphens you find.

2) If there is a hyphen as the last char in the given string, append it onto the exp[] string.

If you want to keep each work separate at first, I'd use a 2D array (with rows and columns), and have one word per row.

word1
word2
word3
...

Otherwise, you can use one single array to hold everything:
word1word2word3...

Whichever way you do it, remember that if you want to use any string functions (seems like you might want to use strcat()), you need to add the '\0' (end of string marker char), to the end of each string, BEFORE you try to treat the char's as a string.

A collection of letters or char's, are just a bunch of char's, until they have that end of string char on the end - then and only then, are they a C string.

I sort of agree with Adak. I would actually do things a little differently though.

I would make a helper function that can just handle expansion of phrases like "1-9" and "A-Z". Make a function that can expand these and store them into an array argument. Make this easier-to-do function first and make sure that it works well.

Then, in your regular "expand()" function all you need to do is look for those phrases in the shorthand string. If you can do that, you can pass in the phrase to the helper function and some array storage and it will fill it in for you. For things that are definitely not phrases, expand() can just copy things character by character or error out.

For example: If a specific character is a digit or a letter, you will want to check the next and next-next characters to find those phrases. So if you find '0', '-' and '9', then you just copy those into a handy argument string, and pass that and some storage to the helper function... Suppose that it's not that nice, then you will want to make error notifications. Keep going until you run out of stuff to do! Eventually you'll pass back a nice expanded string.

You can use each char as a number - since it IS a number inside the computer. A table of those numbers to char's, can be downloaded easily. Google ASCII table, and keep it handy. It's a must for programming.

Forward series length will be the last char (in the series), ascii number value, minus first char (in the series), ascii number value, plus the number of hyphens in the string on the front and the back of the series, plus one. (plus two if you want to add the end of string char to it).

That is for any one series, where the series uses ONLY letters OR numbers, BUT NOT both in the same series.

For example a-c has a length of three, since 'c' - 'a' is two, and we add +1 to it (or plus two if you want it to be a string). The series -a-d- has length 6, since 'd' - 'a' is 3, and then we add +1 to it, and then we add +2 because it has both before and after hyphen char's on it.

Code:

printf("A - F is %d char's long\n",('F' - 'A')+1);

You want the char's to stay char's for this. You don't need to cast the char's as int's. The char's are already represented as numbers, inside the computer, - that's the ascii character set, (which all computers won't use the exact same character set, btw.)

It's not working right because the way that you are calling sprintf() overwrites the same character over and over. The 'expanded' variable represents the starting point. To fix it I would keep track of where you are supposed to be in expanded with a variable, called i perhaps, and change sprintf() like so.

Code:

sprintf(expanded + i, "%c", start);

Every time you copy a character, i will increase by 1. This is one way you can write the helper function I alluded to. You now just need to write the code to use it.

You can always add the starting hyphen to what will be the final string before you do this part of the algorithm, and add a possible hyphen to the final string if it is supposed to be there at the very end. Unless you are really conservative with memory, you won't even need to reallocate to accommodate the hyphens. And make sure you free the sections after you copy them over to the final string.

Ok so my code functions correctly as is. But at line 35, 57, and 80, I print out the expanded range, when what I really want to do is concatenate it onto a base string, and return that base string to main. My problem is that I'm not sure how to declare the base string, as to not provide for any situation of out of bounds exceptions. Because any amount of ranges is currently acceptable ( and I dont really want to change that ), I'm not sure how to determine the space needed. Any suggestions??