Chinaunix首页 | 论坛 | 博客
  • 博客访问: 484780
  • 博文数量: 76
  • 博客积分: 5196
  • 博客等级: 大校
  • 技术积分: 1414
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-10 18:43
个人简介

转了个圈,又回来了

文章分类

全部博文(76)

文章存档

2013年(1)

2011年(8)

2010年(9)

2009年(22)

2008年(36)

我的朋友

分类: 嵌入式

2009-11-22 15:40:43

字符串是一种特殊的线性表,它的每一个节点都是一个字符。在数据处理、文本编辑、词法扫描、符号处理以及自然语言理解等都是以字符串作为处理的基本数据对象。
 
串的存储结构主要有:
1.顺序存储:
    #define maxsize 1024
    char s[maxsize];
2.堆分配存储
    char *ch;
3.链式存储,这个与单链表相似。但由于存储空间利用率的问题,一般都是采用上面两种方法。
 
串的主要操作有:
1.求串长。
int Strlen(char *s)
{
 int len = 0;
 if (NULL == s)
 {
  return -1;
  printf("\nThe string is NULL");
 }
 while(*s++ != '\0')
 {
  len++;
 }
 return len;
}
 
2.字符串拷贝
//------------------------------------------------------
//Function: StrCopy
//    Copy the source string to the dest string;
//
//Parameters:
//    strDest: the  destination string;
//    strSrc: the source string;
//
//Returns:
//    NULL
//-------------------------------------------------------
void StrCopy(char *strDest, char *strCopy)
{
 assert((strDest != NULL) && (strCopy != NULL));
 char *temp = strDest;
 while ((*temp++ = *strCopy++) != '\0'); //结尾以及赋了'\0'
}
关键点在于:strDest和strCopy都必须指向有效地址。而且strDest必须有足够大的空间。否则会造成越界访问。
 
3.串连接
 
将一个串s2连接到另外一个串s1后面。
//--------------------------------------------------------
//Function: StrConcat
//    concat a sting to the rear of another string.
//
//Parameters:
//    s1: the destination string;
//    s2: another string;
//
//Returns:
//    NULL
//--------------------------------------------------------
void StrConcat(char *s1, char *s2)
{
 int len;
 assert((s1 != NULL) && (s2 != NULL));
 len = Strlen(s1);
 s1 += len;
 while ((*s1++ = *s2++) != '\0');
}
 
4.比较串的大小
//---------------------------------------------------------
//Function: StrCompare
//    compare two strings;
//
//Parameters:
//    s1,s2
//
//Return:
//    if s1>s2, return 1; if s1//----------------------------------------------------------
int StrCompare(char *s1, char *s2)
{
 assert((s1 != NULL) && (s2 != NULL));
 while (*s1 != '\0' && *s2 != '\0')
 {
  if (*s1 > *s2)
  {
   return 1;
  }
  else if (*s1 < *s2)
  {
   return -1;
  }
  else
  {
   s1++;
   s2++;
  }
 }
 if (*s1 == '\0' && *s2 == '\0')
 {
  return 0;
 }
 else if (*s1 = '\0')
 {
  return -1;
 }
 else
 {
  return 1;
 }
}
 
5.字串定位
//-----------------------------------------------
//Funciton: StrIndex
//    get the index of string t in the string s.
//
//Parameters:
//    s,t
//    pos: from pos go.pos start from 1, not 0;
//Return:
//    if successed, return the index. else return 0;
//-----------------------------------------------
int StrIndex(char *s, char *t, int pos)
{
 int i = pos-1; //pos start from 1, not 0
 int j = 0;
 int lenS, lenT;
 lenS = Strlen(s);
 lenT = Strlen(t);
 while(i < lenS && j < lenT)
 {
  if (*(s+i) == *(t+j))
  {
   i++;
   j++;
  }
  else
  {
   i = i-j+1; //next round;
   j = 0;
  }
 }
 if (j == lenT)
 {
  return i-lenT+1;
 }
 else
 {
  return 0;
 }
}
关键点就是:用变量i,j来控制结束条件。
然后再通过j来判断是否定位到字串。
 
总结:串的操作中最重要的还是判断串何时结束。
while ((*temp++ = *strCopy++) != '\0');
 
阅读(1955) | 评论(0) | 转发(0) |
0

上一篇:堆栈

下一篇:#ifdef __cplusplus

给主人留下些什么吧!~~