c - remove characters of a char array -


in str char array below first locate first math symbol see, count backwards , remove whatever between previous 3 "_" , remove 3 "_". can please ideas on how this?

so this:

 xa_55_y_*_z_/_+_x_+ 

should turn into:

 xa*_z_/_+_x_+ 

my problem don't know how remove:

_55_y_ 

here code far.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>  int main(int argc, char *argv[]) {      char str [] = "xa_55_y_*_z_/_+_x_+";     int length = 0;     int decrementor = 0;     int underscore_counter = 0;     int = 0;     length = strlen (str);      for(i  = 0; < length; i++)     {         decrementor = 0;         underscore_counter = 0;         if(str[i] == '*' || str[i] == '/' || str[i] == '+' || str[i] == '-')         {             decrementor = i;             while(underscore_counter != 3)             {                 if(str[decrementor] == '_')                 {                     underscore_counter++;                     printf("underscore_counter %d \n", underscore_counter);                 }                 decrementor--;             }         }     }      return 0; } 

you can use strcspn() find first operator, simplifies problem little. it's matter of going backwards , counting underscores, , matter of outputting appropriate substrings, e.g.:

int main() {     char str [] = "xa_55_y_*_z_/_+_x_+";     size_t firstop = strcspn(str, "*/+-");     size_t len = strlen(str);     size_t = firstop;     int underscores = 0;      // go backwards looking underscores (or beginning of     // string, whichever occurs first)     while (i > 0)     {         if (str[--i] == '_')             underscores++;         if (underscores == 3)             break;     }      // output first part of string third underscore     // before first operator     (size_t j = 0; j < i; j++)         putchar(str[j]);      // output rest of string after , including operator     (size_t j = firstop; j < len; j++)         putchar(str[j]);      // , linefeed character (optional)     putchar('\n'); } 

sorry poorly named i , j variables, makes sense.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -