arrays - Need to write a C Program to remove repeated characters adjacent to each other -
i need remove repeated characters adjacent each other.
example: if input "heeellooo wooorllldd"
, output should "helo world"
. output getting "helo wrd"
.
this code have.
#include <stdio.h> #include <string.h> main() { char str[]="heeello wooorld"; redundant(str); } void redundant(char *str) { int check=0; int i,j; char ch; while(str[check]) { ch = str[check]; = j = check + 1; while(str[i]) { if(str[i] != ch) { str[j] = str[i]; j++; } i++; } str[j]='\0'; check++; } printf("string after removing duplicates : %s\n",str); }
i looking minimalistic solution. fun.
void redundant(char *str) { int lastch = -1; /* last read character */ char* inpp = str; /* pointer input location */ char* outp = str; /* pointer output location */ while (*inpp != '\0') { if (*inpp != lastch) { *outp++ = lastch = *inpp; } inpp++; } *outp = '\0'; printf("string after removing duplicates : %s\n", str); }
Comments
Post a Comment