So now I'm going to come down here, and I'm going to read one single character. So I'm going to say character C is going to be assigned in file, and now I'm going to say Get, and this right here will give you one single character. It gives you the ASCII value. Do you see the INT right there? Let me pop that back up. So the Get, it gives you back the INT on that. So it's going to give me the ASCII value.
So I'm going to drop that ASCII value into character variable type, and now I can look at that character. So I'm going to say, if looking-- if we are currently looking for the first character, so if that's true and, at the same time, the character that I just read-- this one right here that I just read from file-- if the character is not equal to a spacebar, and the character is not equal to a new line-- in the line character, right-- if it's not one of those, then that means it must be my first character.
So we found the first character of the sentence, so we can capitalize it. So to capitalize it, all we do is simply say the character will become, or is assigned, the uppercase version of the character. So we're going to take that character, and we're going to uppercase it and then assign it right back into itself, so that will overwrite whatever was there originally. So if that was lowercase initially, it's now uppercase. And then, since I found the first character, I'm going to set my flag to false. So I'm going to lower my flag down. So we found the first character, so flip the flag down.
Remember, a flag is a Boolean. We call it a flag, because of mailboxes-- the old mailboxes. In fact, a lot of you may still have mailboxes like this. They have red flags on the side, and if the flag is down, then that means there is no outgoing mail. If the flag is up, then that means that there's outgoing mail, and the post person will stop by and pick up that outgoing mail, even if they don't have anything to put in there.
So now I change the character to an uppercase, I put my flag down, and then I write that character to my output file, like that. So I'm going to say, write the character to the new file, and that's how it's done. So that right there is just if we're looking for first character and, at the same, time it's not a spacebar, and it's not an end line.
So now what else? If it's not the first character, then we need to go ahead and do some additional work. And we're going to say, if the character is identically equal to a spacebar or the character is identically equal to a new line, then we don't want to do anything with it. We just want to write it to the file, and that's it. So we're going to write spaces and new lines to the new file. And to do that is simply our output file stream. We're going to send the character directly to it, and that's it.
Now, what if we hit a period? So if we hit a period, we know it's an end of sentence, right? So if the character is a period, then we know it's the end of the sentence. And notice that working with characters, we have to put characters in apostrophes. So be careful with that. If you put a quote, it will crash. Because if you put a quote, it's a string, and a string is an array of characters. It's not a single character, it's an array of characters.
So we're going to pop over here, and we're going to say, if it is a period, the period is an end of the line, so that means we're now looking for the first character to uppercase it, so we need to flip our flag. So the period is end of line, so we are now looking for the first character of the next line. So I'm going to flip my flag to true, because now we're looking for the first character, and then that period-- I still need to write that period to the new file. So I'm going to write the period to the new file.
Now, I need to have an Else, and the Else is for all other characters. So I'm not looking for the first character, because I already found the first character, so all other characters, what do we need to do? We've got to lower case them, so we need to lowercase all other letters and write them to the new file. So I'm going to say the character becomes the lowercase version of the character, and then I'm going to write it to the file, like that.
So now that will take care of every other character, and it will lower case it. Looks good. We've got our spaces taken care of, we've got our new line taken care of. It looks good. So now we want to close the pipes, so I'm going to say, end file dot close, and then my outfile-- I need to close that also, right? Like that, and then I want to pause, so there's my pause, and let's see. I'm trying to think of the logic. I think I'm done.
So I got the input from the user and opened the file. I got the output name from the user and then opened it. I set up a flag, and right from the very get go, I need to be looking for the first character. And then I start reading my file, and I check and see if there's more data, and if there is then I'm going to read the character into a character variable. And then if I'm looking for the first character, and I don't hit a spacebar, and I don't hit a new line, then I have the first character. So I upper case it, I flip my flag down, then I write the uppercased character to my file.
Let's see, I think I'm going to move these two lines just for logic because, remember, as we're-- and it would work if I left, but you're looking at making sure that your application is efficient, and that means that it's also maintainable. So it's efficient now, and it's efficient in the future, so it's got to be maintainable.
You're constantly looking at how can I make my application faster and more maintainable. And sometimes it's a balancing act. Sometimes you do some things to make it faster that actually make it a little bit harder to maintain, and sometimes you make things easier to maintain, which costs you a couple cycles against your processor. So it's a balancing act that you're doing. But this right here, the logic behind it, is I uppercase a letter and then right away I write it to my file. And then I flip my flag, and I think I like that better.
So if it's not the first case, if it's not the first character in the sentence, then is it a spacebar? Is it a space bar, or is it a new line? If it's a spacebar or new line, I want to write it directly to the file. Otherwise, is it a period? If it's a period, then that means it's the end of the line. So the next character may be my uppercase character. So I'm going to flip my flag to true, because the next character or the character after that may be my first character for the sentence.
So I'm going to flip my flag to true, because now I'm looking for the first character. And then that period, I need to write that to the file. In all other cases-- so that's why it's the Else-- in all other cases, I want to lowercase the character and then write the character to the file. And then I remembered to close my pipes, so that looks good.
Let's run it. Let's see what happens. So the file name is-- I'm going to type in sentences dot txt, and then the output file name, I'm going to type fix dot txt. Press any key to continue. So I'm going to minimize this, I'm going to go over here, and there's fixed. I'm going to open that up. This is a sentence, This is the second sentence, this is the third sentence, and then here is the next paragraph. Man, that's nice.
Let me open up the original to show you why I'm all excited. Programming is a lot of fun. Check it out. Look how bad this looks, and look how professional this looks. Now, if I wanted to, I could also come in and then make sure that there's only one space or two spaces after the period, like that. And if I did that, then I could clean this up.
Some English instructors will tell you there should only be one space after the period. Some English instructors will tell you that there should be two, so one or two should be fine, but that's not part of this programming challenge, so I don't think I'm going to do it, but it would be fairly easy to do it.
Then other things like this, we can add additional things like that. So your client will say, this is what I want, and then you accomplish it, and then you give it to the client. Then they use it for six months a year, and then they call you back and they say, you know what? We need you to stop by again and do some more work for us, because we also want this. The application that you created for us is fantastic, but now we want this. We want it even better for what we're after. And that's what consulting is about, because people constantly want things to be improved, and they keep hiring us over and over, and that gives us a career.
[End of Audio]