c language string

String (string) is a finite sequence of zero or more characters, also called strings

Generally expressed as

= S "A . 1 A 2 A . 3 A . 4..... A n- " where S is the string name, string together double quotes is a string value, (Some single quotes) quote is not in itself string value A1 can be alphanumeric symbols, called n string length of the string, character string of zero is called an empty string (null string) is expressed as "" "" (4 marks) or "[Phi]" indicates, a zero length string.

Strings being any number of consecutive character strings main strings are e.g. S = "absjasdaasjlask" (main string) = S "SJA", S = "bsjasd" = S " [Phi]", and so are the main string string (any string is empty string string)

Therefore, the structure of the string to be stored in the current string of the space using an array (malloc also apply in the heap memory) according to a required length integer string length (the length of the string does not include '\ 0')

typedef struct Str
{
    char elem[SIZE];
    int length;//没有'\0'
}Str;

String manipulation functions are to

void StrAssign (Str s *, const  char * chars); // initialize string 
void StrCpy (Str s *, t * Str); //   the copy to t s serial string 
BOOL the IsEmpty (Str * s); //      determines whether empty string 
int a GetLength (Str * s); //    returns the length of the string 
void the Clear (Str * s); //       empty string 
BOOL SubStr (Str * Sub, Str s *, int POS, int len); //   from s inside the extraction position pos substrings of length len placed inside the sub 
BOOL the insert (Str S *, int pos, Str * t); //    inserted sequence from position pos t 
int the BF (Str * S, Str sub *,int pos); //    Find position pos in the string s whether there is sub string equal to its index returns 
BOOL DeletePos (Str * s, int pos, int len); //    delete from the length len pos position s 
BOOL the delete (Str S *, T * Str, int pos); //      from the position pos deleted substrings T 
BOOL the replace (Str S *, T * Str, Str v *, int pos); //    with v replaced by pos the first starting position t 
BOOL the ReplaceAll (Str * S, Str t *, V * Str); //       all replaced t V 
void Show (Str S *);

Concrete realization function

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<unistd.h>
#define SIZE 20
using namespace std;
typedef struct Str
{
    char elem[SIZE];
    int length;//没有'\0'
}Str;
void StrAssign(Str *s,const char *chars) // 初始化串,
{
  assert(s!=NULL&&chars!=NULL);
  int len=strlen(chars);
  if(len>SIZE)
  {
    return;
  }
  int i=0;
  for(;i<len;i++)
  {
    s->elem[i]=chars[i];
  }
  s->length=len;
}
void StrCpy(Str *s,Str *t)//  把t 串拷贝到s 串
{
  if(t->length>s->length)
  {
    return;
  }
  int i=0;
  for(;i<t->length;i++)
  {
    s->elem[i]=t->elem [I]; 
  } 
  S -> T-length => length; 
} 
BOOL the IsEmpty (Str S *) //      determines whether an empty string 
{
   return S-> length == 0 ; 
} 
int a GetLength (Str S *) //    returns the length of the string 
{
   return S-> length; 
} 
void the Clear (Str S *) //       empty string 
{ 
  S -> length = 0 ; 
} 
BOOL SubStr (Str * Sub, Str S *, int POS, int len) / /   extracted from the inside position pos s length len substring into which sub 
{
  if(pos<0||len<0||pos>s->length||len>sub->length||len>s->length)
  {
     return false;
  }
  int i=0;
  for(;i<len;i++)
  {
    sub->elem[i]=s->elem[pos+i];
  }
  sub->length=len;
  return true;
}
bool Insert(Str *s,int pos,Str *t)//   从pos 位置插入串t
{
  if(pos<0||pos>s->length||t->length+s->length>SIZE)
  { 
    Return  to false ; 
  } 
  int I = S-> Length- . 1 ;
   for (; I> = POS; i-- ) 
  { 
    S -> elem [I + T-> length] = S-> elem [I]; 
  } 
  for (I = 0 ; I <T-> length; I ++ ) 
  { 
    S -> elem [POS + I] = T-> [I] elem; 
  } 
  S -> T-length + => length;
   return  to true ; 
} 
int the BF (Str * s, Str sub *, int pos) //    Find position pos s string and whether a string is equal to return to its sub superscript 
{
   iF (pos < 0||pos>s->length)
  {
    return -1;
  }
  int i=pos,j=0;
  while(i<s->length&&j<sub->length)
  {
    if(s->elem[i]==sub->elem[j])
    {
      i++;
      j++;
    }
    else 
    {
      i=i-j+1;
      j=0;
    } 
  }
  if(j>=sub->length)
  {
    return i-j;
  }
  else 
  {
    return -1;
  }
}
bool DeletePos(Str *s,int pos,int len)//   从s 的pos 位置删除len 个长度
{
  if(pos<0||pos>s->length||len>s->length-pos)
  {
    return false;
  }
  int i=pos;
  for(;i<s->length-len;i++)
  {
    s->elem[i]=s->elem[i+len];

  }
  s->length-=len;
  return true;
}
BOOL the Delete (Str S *, T * Str, int pos) //      removed from the position of the substring pos T 
{
   int n-= the BF (S, T, pos);
   int len = T-> length;
   BOOL Sign = DeletePos (S , n-, len);
   return Sign; 
} 
BOOL the replace (Str S *, t * Str, Str v *, int pos) //    with v replacing t starting from a first position pos 
{
   int n-= the BF (S, T, POS);
   BOOL Sign = to false ; 
 Sign = the Delete (S, T, POS);
  IF (Sign) 
 { 
  the Insert (S, n-, V); 
 }
  
  return sign;
}
bool ReplaceAll(Str *s,Str *t,Str *v)//      将所有的t 替换成v
{
  while(1)
  {
  if(Replace(s,t,v,0));
  else 
  {
    break;
  }
  }
  return true;
}
void show(Str *s)
{
  int i=0;
  for(;i<s->length;i++)
  {
    printf("%c",s->elem[i]);
  }
}
int main()
{
    Str s;
    Str v;
    Str t;
    StrAssign(&s,"abcdefgabcad");
    StrAssign(&v,"lllll");
    StrAssign(&t,"bc");
  //  Insert(&s,2,&t);
   // Delete(&s,&t,2);
    ReplaceAll(&s,&t,&v);
  //  int n=BF(&s,&t,1);
  //  cout<<n<<endl;
    show(&s);
    cout<<endl;
    return 0;
}

 

 

 

Guess you like

Origin www.cnblogs.com/lc-bk/p/11627150.html