int main(void) { const char * string={"Hello mu darling"}; //puts("Enter your string here:\n"); // gets(string); printf("Your string :\n%s",string); int symbol = 'l'; puts("\nwe are looking for symbol 'l'"); char * ach; ach = myStrchr(string, symbol); printf ("\nYour symbol is is on position # %d\n",ach-string+1); }
int main() { char inputstr[99]; char findchr; int index = 0; printf("inputstr: "); scanf("%s", inputstr); printf("찾는 문자 <chr> : "); scanf("\n%c", &findchr); myStrchr(inputstr, findchr, &index); if (index != -1) { printf("%c의 위치 : %d \n", findchr, index); } else { printf("%c의 위치 : 찾고자 하는 문자 '%c'가 존재하지 않습니다. \n", findchr, findchr); } }
char* myStrtok(char *str, char *delimiters){ static char *current, *next, *endStr; /*Check if this is the first call for a string*/ if(str != NULL){ /*If so, compute the pointer to the position of the '\0' character (string terminator) -> endPosition*/ endStr = str + myStrlen(str); /*Set the next pointer to be returned and the to be the beginning of the string*/ next = str; /*Skip the charactern in the beginning of the string which are delimiters*/ while(next < endStr && myStrchr(delimiters, *next)) next++; /*If the end of the string was reached return NULL*/ if(next == endStr) return NULL; current = next; } else{ /*Return the NULL pointer if the string cannot be split anymore*/ if(current == endStr){ return NULL; } /*Get the next part of the string*/ current++; /*Save the pointer to it to next*/ next = current; } /*While the end of the string hasn't been reached and a delimiter hasn't been found keep iterating*/ while(*current){ /*If the current substring is valid (starts with a non-delimiter, ends right before a delimiter) Stop iterating*/ if(!myStrchr(delimiters, *next) && myStrchr(delimiters, *current)) break; /*If next points to a delimiter, skip to the following character*/ if(myStrchr(delimiters, *next)){ next = current; } current++; } /*Make the character at the current position a string terminator*/ *current = '\0'; /*Return the pointer to the next part of the string*/ /*CORNER CASE: current reached the end of the string, but the character to which next is pointing is a delimiter CORNER CASE: next reaches the string terminator*/ return myStrchr(delimiters, *next) || *next == '\0'? NULL : next; }