筆記試験:文字列の数値を削除し、文字列を圧縮します.


/***************************************************************************************
               
  :                。
    ”abc123de4fg56”     ”abcdefg”。       。
  :              ,
            ,           。           。
    ,        。
*****************************************************************************************/
#include <stdio.h>
#define USE 1
#if USE
void ddc(char *s)
{
 int i = 0;
 int j = 0;
 while(s[i])
 {
  if(s[i]<'0' || s[i]>'9')
  {
   s[j++] = s[i];
  }
  i++;
 }
 s[j] = '\0';
}
#else
char *ddc(char *s)
{
 char *p = s;
 char *res = p;
 while(*s)
 {
  if(*s < '0' || *s > '9')
  {
   *p++ = *s;
  }
  s++;
 }
 *p = '\0';
 return res;
}

#endif

int main(void)
{
    //char *str = "abc123de4fg56"; segmentation fault。
    char str[] = "abc123de4fg56";
    #if USE
    ddc(str);
    printf("result is %s
",str); #else printf("result is %s
",ddc(str)); #endif return 0; }