Reputation: 11
char capSentStart(char paragraph[], char newParagraph[])
{
for(int i = 0; i < 301; ++i)
{
if(paragraph[i] == '.' && paragraph[i + 1] == ' ')
{
newParagraph[i] = '.';
newParagraph[i + 1] = ' ';
newParagraph[i + 2] = ' ';
newParagraph[i + 3] = toupper(paragraph[i + 2]);
++i;
}
else
{
newParagraph[i] = paragraph[i];
}
}
}
If you input say the "paragraph" Dude. dude. dude. dude. and then cout << newParagraph; it prints the same paragraph with nothing changed.
Upvotes: 1
Views: 102
Reputation: 1025
char capSentStart(char paragraph[], char newParagraph[])
{
int index=0;
for(int i = 0; i < 301; ++i)
{
index=i;
if(paragraph[i] == '.' && paragraph[i + 1] == ' ')
{
newParagraph[i] = '.';
newParagraph[i + 1] = ' ';
newParagraph[i + 2] = ' ';
newParagraph[i + 3] = toupper(paragraph[i + 2]);
index=i+3;
}
else
{
newParagraph[i] = paragraph[i];
}
i=index;
}
}
Upvotes: 0
Reputation: 19764
As per your code, if the current character is "."
, then you are doing what you want inside the if condition
. Consider the cases, when the current character is not "."
. You are merely copying the original paragraph which replaces the previous characters in the newParagraph
.
Say input: Dude. dude.
When the current character is the first .
, the newParagraph
at the end of iteration is:
Dude. D___
where _
indicates the characters yet to be filled.
But, in the next four iterations, we don't find a .
. So, you are copying the data from the input
into the newParagraph
thus over-writing the changes.
Dude. dude.
EDIT:
Your code is trying to convert the first character of every sentence to upper Case. Here is how it should be modified:
char capSentStart(char paragraph[], char newParagraph[])
{
int new_paragraph_idx=0;
for(int i = 0; i < 301; ++i)
{
if(paragraph[i] == '.' && paragraph[i + 1] == ' ')
{
newParagraph[new_paragraph_idx++] = '.';
newParagraph[new_paragraph_idx++] = ' ';
newParagraph[new_paragraph_idx++] = ' ';
newParagraph[new_paragraph_idx++] = toupper(paragraph[i + 2]);
++i;
}
else
{
newParagraph[new_paragraph_idx++] = paragraph[i];
}
}
}
Put a new_ptr
which actually fills the newParagraph
separately, thus preventing the over-writing. And also take care of the bounds of the strings. Otherwise you might end up with a SIGSEGV
Upvotes: 2
Reputation: 104
Executing the first iteration in if(paragraph[i] == '.' && paragraph[i + 1] == ' ')
, gives:
paragraph[i] = '.'
^
paragraph = "Dude. dude. ..."
newParagraph = "Dude. D "
After ++i
and the increment of i in the for
loop, i
moves to the following:
paragraph[i] = 'd'
^
paragraph = "Dude. dude. ..."
newParagraph = "Dude. D "
But this time, the iteration executes the code in else
.
else
{
newParagraph[i] = paragraph[i];
}
This changes the newParagraph[i]
which is D
to paragraph[i]
, which is d
.
paragraph[i] = 'd'
^
paragraph = "Dude. dude. ..."
newParagraph = "Dude. d " //'D' is changed back to 'd'
Afterwards, newParagraph
just get overwritten into paragraph
.
Upvotes: 0
Reputation: 1
Use following:
char capSentStart(char paragraph[], char newParagraph[])
{
for(int i = 0,j = 0; i < 301; ++i,++j)
{
if(paragraph[i] == '.' && paragraph[i + 1] == ' ')
{
newParagraph[j] = '.';
newParagraph[j + 1] = ' ';
newParagraph[j + 2] = ' ';
newParagraph[j + 3] = toupper(paragraph[i + 2]);
j=j+3;
i=i+2;
}
else
{
newParagraph[j] = paragraph[i];
}
}
}
Upvotes: 0
Reputation: 1821
You are always writing based on the index i
. Even if you write on higher indexes in newParagraph
, then the follow iterations will overwrite what was done.
Upvotes: 0
Reputation: 1441
In the True case of the if statement you are filling newParagraph but when you keep itterating you are overwriting them again in the false case of the if statement. You should have two itterating variables for Paragraph and new Paragraph.
In order to give you a more detailed answer you should tell us the expeced output.
Upvotes: 0