-4

i'm trying to make a program that if the previous letter is the same with the current letter that will be outputed will put a space between them...

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define T 100
#define S 255

int main(){
char Arrayinput[T][S];
char Arrayoutput[T][S*5];
int i, c, x;
for(i=0;i<T;i++){
    for(c=0;c<S*5;c++){
        Arrayoutput[i][c]=0;
    }
}
scanf("%d", &x);
getchar();
for(i=0;i<x;i++){
        gets(Arrayinput[i]);
}
for(i=0;i<x;i++){
    for(c=0;c<strlen(Arrayinput[i]);c++){
            switch(toupper(Arrayinput[i][c])){
            case 'A':
                if(Arrayoutput[i][c-1]=='2')
                    strcat(Arrayoutput[i]," ");
                strcat(Arrayoutput[i],"2"); break;
            case 'B':
                if(Arrayoutput[i][c-1]=='2')
                    strcat(Arrayoutput[i]," ");
                strcat(Arrayoutput[i],"22"); break;
            case 'C':
                if(Arrayoutput[i][c-1]=='2')
                    strcat(Arrayoutput[i]," ");
                strcat(Arrayoutput[i],"222"); break;
}    }    }

for(i=0;i<x;i++){
    printf("case #%d:%s\n",i+1,Arrayoutput[i]);
}

system("pause");
return 0;

for some case..

if i input.. bca it out puts 22 222 2 which is correct.. such as cab = 222 22 2 bac = 22 2 222

but when ever i input abc it out puts... 2 22222... i dont know why... pls help?

HerlDerp
  • 47
  • 1
  • 1
  • 11

1 Answers1

0

Simply because of the if(Arrayoutput[i][c-1]=='2') of 'C' case with input abc will check always c-1 position, that is always " ".

  • case A -> "2"
  • case B -> "2 22"
  • case C -> Arrayoutput[i][c-1] is Arrayoutput[i][1] that is a space then no space added at the end of the string.

Try this

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define T 100
#define S 255

int main(){
char Arrayinput[T][S];
char Arrayoutput[T][S*5];
int i, c, x;
int len;
for(i=0;i<T;i++){
    for(c=0;c<S*5;c++){
        Arrayoutput[i][c]=0;
    }
}
scanf("%d", &x);
getchar();
for(i=0;i<x;i++){
        gets(Arrayinput[i]);
}
for(i=0;i<x;i++){
    for(c=0;c<strlen(Arrayinput[i]);c++){
        len = strlen(Arrayoutput[i]);
            switch(toupper(Arrayinput[i][c])){
            case 'A':
                if((len != 0) && (Arrayoutput[i][len-1]=='2'))
                    strcat(Arrayoutput[i]," ");
                strcat(Arrayoutput[i],"2"); break;
            case 'B':
                if((len != 0) && (Arrayoutput[i][len-1]=='2'))
                    strcat(Arrayoutput[i]," ");
                strcat(Arrayoutput[i],"22"); break;
            case 'C':
                if((len != 0) && (Arrayoutput[i][len-1]=='2'))
                    strcat(Arrayoutput[i]," ");
                strcat(Arrayoutput[i],"222"); break;
}    }    }

for(i=0;i<x;i++){
    printf("case #%d:%s\n",i+1,Arrayoutput[i]);
}

return 0;
}

OUTPUT

enter image description here

LPs
  • 16,045
  • 8
  • 30
  • 61
  • have you tried the code? because... it really does ticks me.. abc should be = 2 22 222 but print outs 2 22222 and others prints fine.. why is that thats all, serously.. yes it wont put if its not '2' but why doesn't it go through abc=2 22222 as it should be what i want it be? – HerlDerp Jan 30 '15 at 15:24
  • The posted code works as you need. abc = 2 22 222. Compiled gcc 4.7.2 on Debian – LPs Jan 30 '15 at 15:29
  • I edited my answer. You have to think step by step. As edited the third check cheks the second char of the string that is "2 22". Then the if in case 'C' evaluets the space that is not == '2', the no space added at the string before to add "222" – LPs Jan 30 '15 at 15:59
  • i did found an alternative way.. though.. just printing them immedately. – HerlDerp Jan 31 '15 at 04:34
  • Your question was about to explain you why your code was not working. I did it, and corrected your code. To have the same output with a different algoritm is another story, because of your code has 1000k of wrong things... – LPs Jan 31 '15 at 07:52