How upper case every character after period (punctuation) mark

How upper case every character after period (punctuation) mark
0

#1

Hi,

I’m trying to improve my logical thinking within loops by retrieving data and manipulating array elements.

The code that i’m working on is given below.

            string s = "aaaaaaa.   bbbbbbbb.      cccccccc.";
            char[] c = s.ToCharArray();

            for (int i = 0; i < c.Length; i++ )
            {

                if (c[i] == '.')  // (step 1) condition that catch a period punctuation.
                {
                i++// increment the index by one and move to the next element then let WHILE loop do the rest.
                    while (c[i]== ' ') //(step 2)
                    {
                     i++ //As long as while loop encountering an empty element it will be incremented by one     
                    }
                }
             }   
            

I have tried to solve this mystery for 2 days with different angels but with no luck. As you can see it’s a prototype and the code works perfectly according to my debugger. The main problem is when I declare a variable inside the while loop to get the targeted index that I want to upper case. In addition, I can’t retrieve a nested value outside as far as I know.

Anybody curious to solve this issue or giving thoughts about it.


#2

This is… Java? I’m honestly surprised that this works since there’s no bounds checking in your inner loop. How are you not getting something like an “index out of bounds” exception? I’m not a Java expert, but it seems like i should be blowing past the length of the character array.

I would get rid of your while loop and change the if statement. Don’t manually increment your iterator if you’re using a for loop - it doesn’t make your code run any faster, but it’s a lot harder to read and think about. Instead, you could just use the for loop and a boolean flag that will determine if you should uppercase a character. The flag starts as false, but when you hit a period, it turns true. After you uppercase the next valid character, set it back to false. That’s how I would do it, but it’s probably not the only way.


#3

Thanks PortableStick,

Yes, It will throw an exception because It’s merely a prototype. Also, regarding your question about the code it’s a C# code. I will try what you have suggested.