Respuesta :
Answer:
Here is the complete function:
void MakeSentenceExcited(char* sentenceText) { // function that takes the text as parameter and replaces any period by an exclamation point in that text
int size = strlen(sentenceText); //returns the length of sentenceText string and assigns it to size variable
char * ptr; // character type pointer ptr
ptr = sentenceText; // ptr points to the sentenceText string
for (int i=0; i<size; i++){ //iterates through the sentenceText string using i as an index
if (sentenceText[i]=='.'){ // if the character at i-th index of sentenceText is a period
sentenceText[i]='!'; } } } //places exclamation mark when it finds a period at i-th index of sentenceText
Explanation:
The program works as follows:
Suppose we have the string:
sentenceText = "Hello. I'm Miley. Nice to meet you."
The MakeSentenceExcited method takes this sentenceText as parameter
int size = strlen(sentenceText) this returns the length of sentenceText
The size of sentenceText is 35 as this string contains 35 characters
size = 35
Then a pointer ptr is declared which is set to point to sentenceText
for (int i=0; i<size; i++) loop works as follows:
1st iteration:
i=0
i<size is true because i=0 and size = 35 so 0<35
So the body of loop executes:
if (sentenceText[i]=='.') statement checks :
if (sentenceText[0]=='.')
The first element of sentenceText is H
H is not a period sign so the statement inside if statement does not execute and value of i increments to 1. Now i = 1
2nd iteration:
i=1
i<size is true because i=1 and size = 35 so 1<35
So the body of loop executes:
if (sentenceText[i]=='.') statement checks :
if (sentenceText[1]=='.')
This is the second element of sentenceText i.e. e
e is not a period sign so the statement inside if statement does not execute and value of i increments to 1. Now i = 2
So at each iteration the if condition checks if the character at i-th index of string sentenceText is a period.
Now lets see a case where the element at i-th index is a period:
6th iteration:
i=5
i<size is true because i=5 and size = 35 so 5<35
So the body of loop executes:
if (sentenceText[i]=='.') statement checks :
if (sentenceText[5]=='.')
This is the character at 5th index of sentenceText i.e. "."
So the if condition evaluates to true and the statement inside if part executes:
sentenceText[i]='!'; statement becomes:
sentenceText[5]='!'; this means that the character at 5th position of sentenceText string is assigned an exclamation mark.
So from above 6 iterations the result is:
Hello!
This loop continues to execute until all the characters of sentenceText are checked and when the value of i gets greater than or equal to the length of sentenceText then the loop breaks.
The screenshot of the program along with its output is attached.