Join 109,300 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,209 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
I'm new to C programming and trying to do something.
I have a character string. Its just numbers delimited by a comma.
Example.
unsigned char string[] = {1,2,3,4,5,6};
I need to count how many items there are. This is for a function that needs to know how many items its getting. There are more strings so Its not a fixed value. And I can't just use a maximum possible. I know I need to use string.h functions... my understanding is that I would do something like:
get length of whole string. look for first comma add to counter variable. look for next, repeat till end. can some one give me an example of the code?
or of there is an easier way I would appritiate any help. Thanks.
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
First, just to be clear - the example you've provided is not a string, since there is no null-terminating character at the end. (its just a plain character array with 6 characters in, whose character codes are 1, 2, 3 , 4, 5 and 6)
Did you mean to do something like this?
CODE
char string[] = "1,2,3,4,5,6";
Putting anything inside double quotes creates a string literal, which is automatically null terminated (there's an invisible 'nul' character at the end which signals the end of the string to functions such as strlen or printf)
The method you've explained sounds reasonable to me. The best thing to do when you come up with an idea is to just try to implement it and see the result for yourself
First, just to be clear - the example you've provided is not a string, since there is no null-terminating character at the end. (its just a plain character array with 6 characters in, whose character codes are 1, 2, 3 , 4, 5 and 6)
Did you mean to do something like this?
CODE
char string[] = "1,2,3,4,5,6";
Putting anything inside double quotes creates a string literal, which is automatically null terminated (there's an invisible 'nul' character at the end which signals the end of the string to functions such as strlen or printf)
The method you've explained sounds reasonable to me. The best thing to do when you come up with an idea is to just try to implement it and see the result for yourself
Right my bad it is an array, I reference the array later in my code. My problem is that that character array can change so when I'm looping through the array to get the characters I of course need to specify the length of the array. maybe it would be easier if i explained what Im doing.
The characters in the character array represent ascii characters, I then take the codes and use them to assemble a character string. So that's why the array changes. It's length depends on length of text. Im just not sure how to go about breaking up the character array that I manually enter to count how many total ascii codes there are. of course I can count them as I enter them, but its just a pain to do it, if i want to change soemthing in middle of character array, maybe add a word, or spaces and then to recount the whole thing.... the array is not as small as one in example. I just wanted to make a function that would count and set a variable with that number that I then use in the loop.
And to the admins, I understand, Its just that there is a lot of code and its broken up all over place. I could give example, I just figured I could just ask real quick.
Would this code work? Again, I have no way to test this right now. This code is for a PIC chip, and I can't get it into debug mode and I have no way to do any kind of output to console or anything.
I strongly suggest you stop what you're doing and use a desktop PC with a compiler to try out some toy programs with which to get your head around arrays, since it seems that you're just taking wild guesses at the moment.
Take the opportunity to learn how arrays work in C, then try to write a program using an ordinary computer to mimic what you wish to do with your chip/microcontroller. When you're satisfied with the program's behaviour, rewrite it for the microcontroller (If you stick with ISO standard C, this should be a very fast and easy change)
This post has been edited by Bench: 8 Aug, 2008 - 01:03 PM
I strongly suggest you stop what you're doing and use a desktop PC with a compiler to try out some toy programs with which to get your head around arrays, since it seems that you're just taking wild guesses at the moment.
Take the opportunity to learn how arrays work in C, then try to write a program using an ordinary computer to mimic what you wish to do with your chip/microcontroller. When you're satisfied with the program's behaviour, rewrite it for the microcontroller (If you stick with ISO standard C, this should be a very fast and easy change)
yes that what makes it so much harder for me to do this, i have no way of doing any kind of debugging output. Funning thing is ... once this is done i can, all this is to get the LCD display to work. Im working with a PIC chip and programming the display, the way this display works is confusing, I finally got it though, after 4 days of experimenting I got everything on screen to alignt. Which is why i need to know how many elements there are in teh arrray, so I can pass that to the function. And yes I know i need to read more into arrays. I have general knowledge that im getting by with but im starting to get into some complicated manipulation. I was just hopping to finish this part of the program before weekend. I wish I had a shell account somewhere I could use to do some experiments on. I may download knoppix or something this weekend so I can do that. Thank you for the input and now bashing my stupidity I've learned a lot already in last few days on arrays. Now I need to learn to manipulate strings and arrays and the conversion back and forth. Any way Im having a lot of fun, every time I accomplish something the way i predicted its giving me more confidence. Thank you.
You may write C programs which use arrays and perform string manipulation on any system with a C compiler - which is what I suggest you do, instead of trying to guess what's happening on your microcontroller, you'll get a much clearer idea of what you're doing with an ordinary console program.
People who write embedded applications always write a console version to test with, before committing to the hardware, because as you point out, its difficult to perform tests and debug when you can't see what's going on.