I am writing a code that requires me to search for words within a text document. I had to build up words one character at a time in order to ignore punctuation when I search for the words. I did this without a problem. But now I am working on building the output again. This is the code I have that outputs the text. Flag is 1 when a keyword is found and substituted. Flag 0 is when no word is found, and it outputs the original word. If the character is not alpha, is substitues the punctuation back in.

                        if (flag == 1){
                            cout << subs[i]<< " ";
                        if (flag == 0){
                            cout << word_in<< " ";
                        if (!isalpha (text[j])){
                            cout << text[j];

This works, but there is a slight problem. I need to someone write in that if the next word is not alphanumeric, I don't want the space after the word. My output has the space before apostrophes and periods. So if I put another if statement after the subs[i] and word_in words that states, if next word is alphanumeric, count " ". But I don't know if I can do this. How would you make a statement about a next letter?