1.对一个字符串进行裁剪
#include <stdio.h>
#include <string.h>
void strcut(char* source, int len,char* end)
{
if(strlen(source)>=len+strlen(end)) //如果源字符串长度超过(给定长度+结束字符串长度 )
strcpy(source+len,end); //在字符串后加上结束字符串,对其进行裁剪
}
int main()
{
char str[256]="I really love huaji.";
strcut(str,10,"...");
puts(str);
return 0;
}
解法二
#include <stdio.h>
#include<string.h>
void copystr(char *p1, char *p2,int m);
int main()
{
int m;
char str1[20],str2[20];
printf("input string1");
gets(str1);
printf("begin with?(input a number):");
scanf("%d",&m);
if(strlen(str1)<m){
printf("error!");
}
else
{
copystr(str1,str2,m);
printf("%s\n",str2);
}
return 0;
}
void copystr(char *p1,char *p2,int m)
{
int n=0;
while(n<m-1)
{
n++;
p1++;
}
while(*p1!='\0')
{
*p2=*p1;
p1++;
p2++;
}
*p2='\0';
}
2.