[Note: This program is verified using vs2013 version]
#include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable:4996) int my_strRidSpace(char *dest,char *src, int *n){ char *buf = src; int begin = 0; int end = strlen(buf) - 1; int len = 0; if (buf == NULL || dest==NULL){ return -1; } //从左边开始 while(buf [the begin] == ' ' && buf [the begin] =! 0 ) { // if the current character is not empty, but does not end the begin ++; // position of the right moving a } the while (buf [End] == ' ' && buf [end] =! 0 ) { // if the current character is not empty, but no end end--; // position of a left movement } len = end - the begin + . 1 ; * = n- len; strncpy ( dest, buf + the begin, len); // strncpy (): copy the string dest, the specified length (not added back 0) dest [len] = 0 ; / *Here own implementation of specified length copy string function for (int I = 0; I <len; I ++) { * dest = * (buf + the begin); dest ++; buf ++; } * / return 0 ; } int main ( void ) {
/ * find non-space character string length, two examples of block model: "asfqwfq" * / char * P = " asdqwfrd " ; char STR [ 50 ] = { 0 }; int n-= 0 ; int RET = 0 ; RET = my_strRidSpace (STR, P, & n-); if (ret != 0){ printf("my_strRidSpace err %d", ret); return ret; } printf("str = %s\n", str); printf("n= %d\n", n);
printf("\n"); system("pause"); return 0; }