Home > OS >  How to add space between the characters if two consecutive characters are equal in c?
How to add space between the characters if two consecutive characters are equal in c?

Time:02-15

I need to add add space if two consecutive characters are same.

For example: input:

ttjjjiibbbbhhhhhppuuuu

Output:

t tjjji ibbbbhhhhhp puuuu

If the two consecutive characters are same then need to print space between two consecutive characters....if the consecutive characters are greater than two no need to add space.

My code:

#include <stdio.h>
#include <string.h>
int main()
{
    char s[100]="ttjjjiibbbbhhhhhppuuuu";
    for(int i=0;i<strlen(s);i  ){
      if(s[i]!=s[i-1] && s[i]==s[i 1]){
        s[i 1]=' ';
    }
}
printf("%s",s);
}

my output:

t j ji b b h h hp u u 

What mistake i made??

CodePudding user response:

Your primary mistake is writing to your input when the string needs to grow. That's not going to work well and is hard to debug.

This is typical of C Code: measure once, process once. Same-ish code appears twice.

Variables:

int counter;
char *ptr1;
char *ptr2;
char *t;

Step 1: measure

for (ptr1 = s; *ptr1; ptr1  )
{
      counter;
    if (ptr1[0] == ptr1[1] && ptr1[0] != ptr1[2] && (ptr1 == s || ptr1[-1] != ptr1[0]))
          counter;
}

Step 2: copy and process

t = malloc(counter   1);
for (ptr1 = s, ptr2 = t; *ptr1; ptr1  )
{
    *ptr2   = *ptr1;
    if (ptr1[0] == ptr1[1] && ptr1[0] != ptr1[2] && (ptr1 == s || ptr1[-1] != ptr1[0]))
        *ptr2   = ' ';
}
ptr2[0] = '\0';

CodePudding user response:

Another solution: Calculate the length of consective characters and handle the special case(Length == 2).

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv) {
  char s[100] = "ttjjjiibbbbhhhhhppuuuu";
  char tmp_ch = s[0];
  int cnt = 1;
  
  for (int i = 1; i < strlen(s); i  ) {
    while (s[i] == tmp_ch) {
      cnt  ;
      i  ;
      if (i == strlen(s)) {
        break;
      }
    }
    if (cnt == 2) {
      putchar(tmp_ch);
      putchar(' ');
      putchar(tmp_ch);
    } else {
      for (int j = 0; j < cnt; j  ) {
        putchar(tmp_ch);
      }
    }

    tmp_ch = s[i];
    cnt = 1;
  }

  return 0;
}

CodePudding user response:

Another approach is to use strspn() to get the number of consecutive characters as you work down the string. The prototype for strspn() is:

    size_t strspn(const char *s, const char *accept);

Where strspn() returns the number of bytes in the initial segment of s which consist only of bytes from accept. (e.g. using the current character in a 2-character string as accept, it gives the number of times that character appears in sequence)

Tracking the number of charters returned and updating an offset from the beginning allows you to simply loop letting strspn() do the work as you work though your string. All you are concerned with is when strspn() returns 2 identifying where two, and only two, of the same character are adjacent to one another.

You can do:

#include <stdio.h>
#include <string.h>

int main (void) {
  
  char *input = "ttjjjiibbbbhhhhhppuuuu";
  char chstr[2] = {0};            /* 2 char string for accept parameter */
  size_t nchr = 0, offset = 0;    /* no. chars retured, current offset */
  
  *chstr = input[offset];         /* initialize with 1st char */
  /* while not at end, get number of consecutive character(s) */
  while (*chstr && (nchr = strspn (input   offset, chstr))) {
    if (nchr == 2) {              /* if 2 - add space */
      putchar (input[offset]);
      putchar (' ');
      putchar (input[offset]);
    }
    else {  /* otherwise, loop nchr times outputting char */
      size_t n = nchr;
      while (n--)
        putchar(input[offset]);
    }
    offset  = nchr;               /* add nchr to offset */
    *chstr = input[offset];       /* store next char in string */
  }
  
  putchar ('\n');                 /* tidy up with newline */
}

Example Use/Output

$ /bin/space_between_2
t tjjji ibbbbhhhhhp puuuu

Let me know if you have further questions concerning the use of strspn().

  •  Tags:  
  • c
  • Related