将字符串用句号分裂开
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxsize 100
void split(char s[])
{ char tmp[maxsize];
memset(tmp, 0, sizeof(tmp));
int count,c;
int i=0;
c=0;
while(i<strlen(s)){
count=0;
while(s[i]!='\0'&&s[i]!='.'){
tmp[count++]=s[i++];
// printf("%c",tmp[count-1]);
}
printf("%s\n",tmp);
memset(tmp, 0, sizeof(tmp));
i++;
}
}
int main(){
char s[]="192.23.32";
split(s);
}
易错点:C语言直接清空数组、字符串
(1)循环方法
char a[ ]="aaaaaaaa"; //定义字符数组
for (unsigned int i = 0; i < strlen(a); i++)
a[i] = '\0' ; //for循环清空数组
(2) memset函数方法
memset包含在头文件string.h中,函数原型为:memset(void *s,int ch,size_t n)。
char a[ ]="aaaaaaaa"; //定义字符数组
memset(a, 0, sizeof a); //清空数组
(3)strcpy
直接使用strcpy将一个空串赋值给字符串就可以,需要string.h
char ss[11] = {"hello world"}; //当前为hello world
strcpy(ss, "");
//现在的ss就是空串了
原文链接:https://blog.csdn.net/cool99781/article/details/106388593