Reputation: 5307
As far as I know the difference between for
loop and while
loop looks like this:
for:
fist initialization takes place then conditional expression is checked and if it results in TRUE
then only statement part gets executed this loop is continuous till conditional expression results FALSE
.
While:
conditional expression is checked first and if result TRUE
then statement part gets executed otherwise not, this loop is continuous till conditional expression results FALSE
.
Today I wrote an Algorithm which check if a string
has duplicates, and if so to print only those which aren't:
#include<stdio.h>
#include<string.h>
int main(void){
const char *str = "Mississippi";
char tmp[15] = {0};
size_t i=0,j=0,k=1;
int found=0;
tmp[0] = str[0];
printf("Before = %s\n",str);
while(str[i] != '\0'){
for(j=0;tmp[j] != '\0';j++){
if(tmp[j] == str[i]){
found++;
}
}
if(found==0){
tmp[k]=str[i];
k++;
}
found=0;
i++;
}
tmp[strlen(tmp)] = '\0';
printf("After = %s\n",tmp);
return 0;
}
Output:
Before = Mississippi
After = Misp
Now watch what happens if replace the for
loop:
for(j=0;tmp[j] != '\0';j++){
if(tmp[j] == str[i]){
found++;
}
}
With a `while loop:
while(tmp[j] != '\0'){
if(tmp[j] == str[i]){
found++;
}
j++;
}
I get:
#include<stdio.h>
#include<string.h>
int main(void){
const char *str = "Mississippi";
char tmp[15] = {0};
size_t i=0,j=0,k=1;
int found=0;
tmp[0] = str[0];
printf("Before = %s\n",str);
while(str[i] != '\0'){
while(tmp[j] != '\0'){
if(tmp[j] == str[i]){
found++;
}
j++;
}
if(found==0){
tmp[k]=str[i];
k++;
}
found=0;
i++;
}
tmp[strlen(tmp)] = '\0';
printf("After = %s\n",tmp);
return 0;
}
But the output is not as expected:
Before = Mississippi
After = Misp
But:
Before = Mississippi
After = Misisipi
Why does this happen?
Upvotes: 0
Views: 120
Reputation: 75854
for ( init-statement(optional) ; condition(optional) ; iteration_expression(optional) ) statement
The above syntax produces code equivalent to:
{ init_statement while ( condition ) { statement iteration_expression ; } }
Except that
- Names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if condition is a declaration) are in the same scope (which is also the scope of statement).
continue
in the statement will execute iteration_expression- Empty condition is equivalent to
while(true)
Upvotes: 1
Reputation: 224852
What's missing is that the for
loop is initializing j
to 0 when it first enters.
Although j
is initialized to 0 when it declared, the while
loop is called within another while
loop, so it doesn't get reinitialized each time.
Add the initialization:
j=0;
while(tmp[j] != '\0'){
if(tmp[j] == str[i]){
found++;
}
j++;
}
And you get:
Before = Mississippi
After = Misp
Upvotes: 2
Reputation: 15951
You never reset j to 0 with the while loop.
This bit:
for(j=0;
Upvotes: 1
Reputation: 15229
Note that the while
loop doesn't initialize j
to 0
.
Remove the initialization of j
to 0
and add
j = 0;
in front of your nested while
loop.
Upvotes: 1
Reputation: 1782
Because the while loop does not initialize j=0
at every iteration of the outermost while loop!
Upvotes: 1