You are not incrementing txtPtr to point to next character. Basically you are converting the same character text_size times. Then searching for it in search array 26 times. Change 45 line to txtPtr = random_text + n; But, there is a better way. You could use the fact that letters a-z can be converted directly to integer 0-25 using ASCII table. I leave the rest for You. A hint about debugger is also useful.