Is there a function like strncmp(str1, str2, n) (im not sure if parameters go in that order) but "n" would be the number where the strings would start to be compared not where they would end comparing?
Printable View
Is there a function like strncmp(str1, str2, n) (im not sure if parameters go in that order) but "n" would be the number where the strings would start to be compared not where they would end comparing?
assuming both strings are at least n characters long:
strcmp(str1+n, str2+n);
alex
yes your right there is a strncmp. Its prototyped like this :-
int strncmp(const char* s1,const char* s2,size_t num);
you will find it in string.h just like the other string functions.It will compare up to num chars from the two null-terminated strings. return values are as for strcmp.
I have a similar question. If I find a string using strstr(str1, sentence) how would I be able to get the position number of where that word begins?
eg. strstr("Person walking.", "walking"); /* walking begins at position 7 or 8 depending if counting starts from 0 */
Then I would assign i=/* the return value of position of that special function */
strstr return a pointer to the begining of the sub string. At least that's how I remember it. So than assign a pointer to the char array:
At least I think something like that should do the job.Code:char array[40] = "My Sentence ...";
char *pstart = array;
//call strstr
char *pend = strstr(array,"Sentence");
int position = pend - pstart;
True. If you get the actual address were the the start of the word is found and then subtract the address were the original array starts that will tell you how many places into the array the search had to go before finding it.