Why the 2nd else if is never working D:

#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;


int main()



{
int j,flag=0;
char ara[10000],arb[10000];
j=0;
gets(ara);
for(int i=0;ara[i];i++)
{
if(ara[i]!='\"')
while(ara[i]!='\"' && ara[i]!='\0')
{
arb[j]=ara[i];
i++;
j++;
}
else if(flag==0 && ara[i]=='\"' )
{
arb[j]='\`';
arb[j+1]='\`';
flag=1;
j+=2;

}
else if(flag==1 && ara[i]=='\"')
{
arb[j]='\'';
arb[j+1]='\'';
flag=0;
j+=2;

}

}
arb[j]='\0';
puts(arb);
return 0;
}
//It is supposed to replace the first quote by `` and the next quote by '' . But , the second process is not working!! Please help me to figure it out :)
is this a guy a beginner?
The problem is the inner loop while. It increments i until ara[i] = '\"'. Then the code skips both if else segments (because it executed first if segment). Outter loop ends and i is incremented again in for(int i=0; ara[i]; i++) one time too many, because it is now one index over the quote.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
  int j=0,flag=0;
  char ara[10000],arb[10000];
  gets(ara);
  for(int i=0;i<10000;i++){
    if(ara[i]!='\"'){
      arb[j]=ara[i]; j++;
      if(ara[i]=='\0') break;
    }else{
      if(flag==0){
        arb[j]=arb[j+1]='\`';
        flag=1;
      }else{
        arb[j]=arb[j+1]='\'';
        flag=0;
      }  i++; j+=2;
    }
  }
  puts(arb);
  return 0;
}


I think you must update index for i and j. j+=2;i++; I make this for all cases above.
Last edited on
Topic archived. No new replies allowed.