本文介绍的是截取字符串。函数说明:设定起始位置和结束位置,然后将字符添加到子字符串数组中,字符为ascii编码,且字符串的长度必须大于等于1,小于等于32767。
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
char* interceptstr(char* string,unsigned int start, unsigned int end)
{
unsigned int i,num1,num2, num3, num4,num5,num6;
bool bool1, bool2, bool3,bool4,bool5,bool6,bool7, bool8;
char* str1 = nullptr,*str2=nullptr;
i = 0;
num1 = 0;
num2 = 0;
num3 = 0;
num4 = 0;
num5 = 0;
num6 = 0;
bool1 = false;
bool2 = false;
bool3 = false;
bool4 = false;
bool5 = false;
bool6 = false;
bool7 = false;
bool8 = false;
bool1 = (string == nullptr);
if (bool1)
{
puts("The string is Empty!\n");
return str1;
}
num1 = strlen(string);
bool2 = num1 == 0;
if (bool2)
{
puts("The length of the string is 0!\n");
return str1;
}
bool3 = (num1 >= 1) && (num1 <= 32767);
if (bool3 == false)
{
puts("The length of the string must greater than or equal to 1 and less than or equal to 32767 !\n");
return str1;
}
bool4 = end >= start;
if (bool4 == false)
{
puts(" Variable end must greater than or equal to variable start.\n");
return str1;
}
num2 = end + 1;
bool5 = num2 > num1;
if (bool5)
{
puts("Sub string exceeds the boundary of source string!\n");
return str1;
}
num3 = end - start + 2;
num4 = sizeof(char);
num5 = num3 * num4;
str2 = (char*)malloc(num5);
bool6 = (num1 == 1) && (start == 0) && (end == 0);
if (bool6)
{
*str2 =*string;
*(str2 + 1) = '\0';
}
bool7 = (num1>1)&&(start == end);
if (bool7)
{
*str2 = *(string+start);
*(str2 + 1) = '\0';
}
bool8 = end>start;
if (bool8)
{
for (i = start; i <= end; i++)
{
*(str2 + num6) = *(string + i);
num6++;
}
*(str2 + num6) = '\0';
}
return str2;
}
int main()
{
char ch01[] = "abcdefghigklmnop", *ch02 = nullptr;
ch02 = interceptstr(ch01,0,6);
printf("ch02 is %s\n", ch02);
return 0;
}