字串的基本操作,c語言實現
話不多說,直接上程式碼。如果有什麼錯誤,直接噴!
#include <stdio.h>
#include <stdlib.h>
#define LEN 100
#define LENINC 10
int main()
{
char string[LEN] ;
char string1[] = {"asdfghjkl"};
char string2[] = {"asdfghjkl"};
printf("\nlength=%d\n",getLength(string1));
printf("\nstrCompare=%d\n",strCompare(string1, string2));
stringConcat(string,string1,string2);
printf("\nstring=%s\n",string);
subString(string);
printf("\nadress=%d\n",BFmatch(string1, string2, 0, 10));
printf("\nadress=%d\n",KMP_function(string1, string2, 0, 10));
stringCopy(string1,string);
printf("string1=%s\nstring =%s\n",string1,string);
return 0;
}
//計算串的長度:
int getLength(char *str)
{
int len=0;
if(NULL == str)
return len;
while(str[len++]!='\0');
return --len;
}
///2.串的比較
int strCompare(char *str1,char *str2)
{
if(NULL == str1 || NULL == str2)
exit(0);
int i = 0;
while(str1[i] == str2[i] && (str1[i] != '\0' || str2[i] != '\0'))
i++;
return (int)(str1[i] - str2[i]);
}
///3.串的連線:
int stringConcat(char *string,char *str1,char *str2)
{
int i = 0, k = 0;
for( ; i < getLength(str1); i++)
{
if(k>getLength(string))
{
string = (char *)realloc(string, sizeof(char)*(getLength(string)+LENINC));
}
string[k++] = str1[i];
string[k] = '\0';
printf("\n%s\n",string);
}
for(i = 0; i < getLength(str2); i++ )
{
if(k>getLength(string))
{
string = (char *)realloc(string, sizeof(char)*(getLength(string)+LENINC));
}
string[k++] = str2[i];
string[k]='\0';
}
return 1;
}
///4.求所有子串。
int subString(char *string)
{
int i = 0 , j = 0 , count = 0 , k = 0;
for(i = 0 ; i < getLength(string) ; i++ )
{
for( j = i ; j < getLength(string) ; j++ )
{
for(k = i ; k < j ; k++)
{
printf("%c",string[k]);
count++;
}
printf("\n");
}
}
return 1;
}
///5.字串匹配:
//演算法一:BF演算法。
/*
int BFmatch(char *str_fat, char *str_son,int pos,int len)
{
int i , j = 0 ;
int len1 = pos+len;
int len2 = getLength(str_son);
for(i = pos ; i < len1 && j < len2 ; i++ )
{
printf("%c-%c",str_fat[i],str_son[j]);
if(str_fat[i] != str_son[j++])
{
j=0;
}
}
if(j == len2)
return i-j+1;
else
return -1;
}
*/
//演算法一:BF演算法
int BFmatch(char *str_fat, char *str_son,int pos,int len)
{
int i = 0 , j = 0 ;
for(i = pos ; i < pos + len ; i++ )
{
for( j = 0 ; j < getLength(str_son) ; j++ )
{
if(str_fat[i + j] != str_son[j])
break;
}
if(j == getLength(str_son))
return i+1;
}
return -1;
}
/**********************比較重要的演算法**********************************/
//演算法二:KMP演算法:
int KMP_function(char *str_fat,char *str_son,int pos)
{
int i = pos;
int j=1;
int a[255] = {0};
getNext(str_son,a);
while(i<=(int)strlen(str_fat)&&j<=(int)strlen(str_son)){
if(0==j||str_fat[i]==str_son[j-1])
{
i++;
j++;
}else{
j = a[j];
}
}
if(j>(int)(strlen(str_son)))
return i-strlen(str_son)+1;
else
return -1;
}
void getNext(char *str_son,int *next)
{
int i=1,j=0;
next[0] = next[1] = 0;
while(i<(int)strlen(str_son))
{
if(0==j||str_son[i] == str_son[j])
{
i++;
j++;
next[i] = j;
}else{
j = next[j];
}
}
}
///6.串的複製:
void stringCopy(char *string,char *t)
{
if(NULL == string)
return ;
int i = 0 ;
for( i = 0 ; string[i] != '\0' ; i++ )
{
t[i] = string[i];
}
t[i] = '\0';
}