我的c語言作業----1
/*作業介紹*/ /***********************************************/ //輸入一字串去點前後的空格 //要求:封裝,檢驗是否滿足要求 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void rem_space(char* input, char* output) { int i=0,j=0; do { if (*(input++) == ' ') { ; } else { output[j++] = *input; } } while (*input != '\0'); } void main() { char *str=" sdfsdf a"; char oupt[100] = {'\0'}; rem_space(str, oupt); printf("%s",oupt); printf("\n"); system("pause");
}
第一次:失敗
打印出來只有dfsdf
分析:
除錯過程中,跳轉沒數清空格個數,誤認為除錯正確
/*作業介紹*/ /***********************************************/ //輸入一字串去點前後的空格 //要求:封裝,檢驗是否滿足要求 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void rem_space(char* input, char* output) { int i=0,j=0; do { if (*(input) == ' ') { input++; } else { output[j++] = *input++; } } while (*input != '\0'); } void main() { char *str=" sdfsdf a"; char oupt[100] = {'\0'}; rem_space(str, oupt); printf("%s",oupt); printf("\n"); system("pause");
}
第二次:成功
分析:
*input 為空格後要跳轉到下一個位置,第一次錯誤是因為,在 if (*(input++) == ' ') 如果是字元,打印出來的是++之後的,所以會缺失一個 s 一個a 並會有亂碼。
待改進:
1.封裝為任意字串2.函式返回值,沒有返回成功與失敗