当前位置:网站首页>[pointer] delete all spaces in the string s

[pointer] delete all spaces in the string s

2022-07-06 14:35:00 |Light|

requirement

Make up a function , Delete string s All spaces in .( Use a pointer to achieve )

Code

/*  This function is used to delete all spaces in the string  a Is a pointer to a string array  */
char *delete_space(char *a)
{
    
    int n = strlen(a);
    int i=0,j=0,k=0;
    char b[n];
    for(i=0;i<n;i++)
    {
    
       if(a[i]==' ')
    {
    
        k++;
       continue;
    }
       else
       {
    
           b[j]=a[i];
           j++;
           a[i]='\0';
       }
    }
    for(j=0;j<n-k;j++)
    {
    
        
        a[j]=b[j];
       

    }    
    return a;
}

main function

 int main()
 {
    
     char a[200];
     gets(a);
     delete_space(a);
     puts(a);
    return 0;
 }   

test

Test input
I A m A Student
Output
IAmAStudent

原网站

版权声明
本文为[|Light|]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060918542230.html