将一个字符串,按照奇数位和偶数位分别存放到不同的字符串中
题目描述:给定一个字符串
要求写一个函数,实现以下功能
功能1:吧偶数位字符挑选出来,组成一个字符串
功能2:吧奇数位字符挑选出来,组成一个字符串
// 将一个字符串,按照奇数位和偶数位分别存放到不同的字符串中 #define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include<string.h> #include<stdio.h> int getStr1Str2(char * source, char *buf1, char *buf2) { char *p = source; int num = 0; //记录函数返回值 if (source == NULL || buf1 == NULL || buf2 == NULL) //判断是否为空指针 { num = -1; printf("func getStr1Str2() err (source == NULL || buf1 == NULL || buf2 == NULL) :%d ", num); return num; } int i = 9; int j = 0; int m = 0; for (m=0,i = 0, j = 1; i < strlen(source) && j < strlen(source);m++) { buf1[m] = source[i]; buf2[m] = source[j]; i = i + 2; j = j + 2; } buf1[m] = '\0'; buf2[m] = '\0'; } void main() { char *str_old = "1a2b3d4c"; char str_new1[100]; char str_new2[100]; getStr1Str2(str_old, str_new1, str_new2); printf("str_old:%s \n", str_old); printf("str_new1:%s \n", str_new1); printf("str_new2:%s \n", str_new2); system("pause"); return; }