c - fill word with blanks -


i have following code strange chars instead of blanks:

char *getword(const char *string) {     char *chars = "-_";     int len = 0;     int max = 10;     char *res = malloc(max + 1);     ( ; len<max; string++) {         if(*string)         {             if (!strchr(chars, *string)) {                 res[len] = *string;                 ++ len;             }         }         else         {             res[len] = ' ';             ++ len;         }     }     res[len] = 0;     return res; } 

ex: char *mystring = "my-str_a"; //the result want be: "mystra" followed 4 blanks: "mystra "

how can fill blanks end of word until reaches max length ?

original answer

one problem missing else clause if (*string), need set character blank. problem read past end of string.

char *getword(const char *string) {     char *chars = "-_";     int max = 10;     int len;     char *res = malloc(max + 1);     if (res == 0)         return res;     (len = 0; len < max; len++)     {         if (*string)         {             if (!strchr(chars, *string))                 res[len] = *string;             else                 res[len] = ' ';             string++;         }         else             res[len] = ' ';     }     res[len] = 0;     return res; } 

this uses conventional for (int len = 0; len < max; len++) loop step through allocated array. code increments string when not pointing @ terminal null byte. there assignment res[len] on each iteration. code checks memory allocation succeeds — important in real programs.

revised answer

the function getword2() require. note test harness.

#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h>  static char *getword1(const char *string) {     char *chars = "-_";     int max = 10;     int len;     char *res = malloc(max + 1);     if (res == 0)         return res;     (len = 0; len < max; len++)     {         if (*string)         {             if (!strchr(chars, *string))                 res[len] = *string;             else                 res[len] = ' ';             string++;         }         else             res[len] = ' ';     }     res[len] = 0;     return res; }  static char *getword2(const char *string) {     char *chars = "-_";     int max = 10;     int len = 0;     char *res = malloc(max + 1);     if (res == 0)         return res;     while (len < max)     {         if (*string)         {             if (!strchr(chars, *string))                 res[len++] = *string;             string++;         }         else             res[len++] = ' ';     }     res[len] = 0;     return res; }   int main(void) {     const char *data[] =     {         "my-str_a",         "--m--__",         "my str a",         "abyssinianelephant",         "--m--__m--m--m-m-m-m-m-m-m-m-m-m",     };      (size_t = 0; < sizeof(data)/sizeof(data[0]); i++)     {         char *res1 = getword1(data[i]);         char *res2 = getword2(data[i]);         char source[30];         snprintf(source, sizeof(source), "<<%.25s>>", data[i]);         assert(res1 != 0 && res2 != 0); // reprehensible!          printf("%-30.30s --> <<%s>> or <<%s>>\n", source, res1, res2);         free(res1);         free(res2);     }     return 0; } 

sample output:

<<my-str_a>>                   --> <<my str  >> or <<mystra    >> <<--m--__>>                    --> <<  m       >> or <<m         >> <<my str a>>                   --> <<my str  >> or <<my str  >> <<abyssinianelephant>>         --> <<abyssinian>> or <<abyssinian>> <<--m--__m--m--m-m-m-m-m-m->>  --> <<  m    m  >> or <<mmmmmmmmmm>> 

if 'mmmmmmmmmm' output isn't want, specification needs tightening bit. tweaks aren't hard, need specified.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -