Although pointers are commonly used in C, you can use an index in a very similar way, to do the same job.
For inexperienced programmers, I believe an index is easier to work with, and to understand:
Say you wanted to find the first 'r':
The variable i will now have the element number of the 'R'.Code:int i=0; char array[] = {"Singing in the Rain"}; while(array[i]) { //array[string length of array[] + 1 will be '\0' if(array[i] == 'r' || array[i] == 'r') break; ++i; }
I'm posting this answer code for you with this understanding - you *will* post up code (or pseudo code), to try and solve your future C programming questions, with your first post in a thread. Don't expect a "codez plz" kind of relationship with the forum.
yeah i know that i have to post code...and this is what i am doing...i just don' t know C very well so i have many troubles...and also, i don' t speek english daily (it is not my "first" language)...this is another problem...but i have to learn C and next year C++ for my university so i just try...thanks for your answer.....
You're basically looping through an array and checking if an element exists in it. Here's a code example:
Code:int strchr(const char *s, const char c) { while (*s) { if (*s == c) return 1; else s++; } return 0; }
Last edited by dxfoo; 06-21-2010 at 05:31 PM. Reason: It's Monday.
Unfortunately, dxfoo's example is fatally flawed due to a misplaced closing brace, but that should be easy to spot and fix (although the resulting code is not quite standard strchr).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Fixed after I posted Simple function, but it explains how the basic algorithm works.