c string help


I'm having trouble understanding c strings. I need to be able accept a string from the user. then count how many numbers are in it and report it back. I'm not really sure where to start with this. i have looked at various articles on c strings and i can't seem to get it.
Last edited on
Okay, first of all a string is basically an array of characters. They can consist of numbers, letter and symbols. So, an example string would be "hfgndjn54545jj". That obviously doesn't mean anything but strings are usually used to hold words or phrases inside them for later use. An important point is that the string "123" is NOT the same as the number 123, so if you added 2 strings together then the string would just literally be placed onto the end of the first one whereas if you add numbers they do just that.

To get a certain character from a string you would write something like this:

1
2
3
char letter;
string temp = "hello";
letter = temp[1];

This code would set the variable "letter" to the second letter in the string "temp" because the index of an array starts at 0. So 0 is "h", 1 is "e", 2 is "l" and so forth.

As for counting how many numbers there are in the string you would use a for loop so you can check each of the individual letters inside the string and check if it is an integer. If it is, then you could increment an integer which counts how many numbers there are.

Hope this makes sense!
Last edited on
Topic archived. No new replies allowed.