Could you please explain why we are incrementing i again and again in line 29 and 31..just this confusion

question:

https://practice.geeksforgeeks.org/problems/print-first-letter-of-every-word-in-the-string3632/1/?category[]=Strings&problemStatus=unsolved&difficulty[]=-1&page=1&query=category[]StringsproblemStatusunsolveddifficulty[]-1page1

answer:

Hey @aarijrab
You can do this in one i++ also

Here see this

class Solution{
public:	
	
	string firstAlphabet(string S)
	{
	    // Your code goes here
	    string str="";
	    if(S[0]!=' ') 
	    str+=S[0];
	    
	    int i=1;
	    while(i<S.length())
	    {
	        if(S[i]==' ')
	        {
	            if(i+1<S.length())
	            {
	                str+=S[i+1];
	                // i++;(we can do this because we know that next char cant be first letter again so you can say its lil bit of optimization)
	            }
	            // i++; (removed from here because in else too we are doing i++
	        }
	        // else  (removed else because we want to do i++ in both anyway)
	        i++;
	    }
	    return str;
	}
};
 

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.