`
chinrui
  • 浏览: 96755 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

字符串功能实现

阅读更多
字符串(C++)

/* 最后一个线性存储结构 */
/* 功能:
   初始化:生成一个任意长度的字符串
   展示:显示字符串的值
   复制:复制一个任意长度字符串的值到另外一个任意长度字符串
   连接:将两个任意长度的字符串连接成一个新和字符串
   比较:比较两个任意长度的字符串的大小
   截取:在一个字符串上截取一个子串
   插入:在一个字符串中插入另外一个任意长度的字符串
   判断包含:判断一个主串是否包含另外一个子串
*/


#include <iostream>
#include <stdlib.h>
#include <stdio.h>

#define MAX_SIZE 5
#define INCREASE_SIZE 10

using namespace std;

typedef struct Node {
    char *data;
    int length;
    int maxSize;
} *String;

//declare the function
String initString();
void display(String);
void link(String , String);
void copyStr(String , String);
int compare(String , String);
String subStr(String , int , int);
void insert(String , String , int);
bool contain(String , String);

int main()
{
    cout << "---------------------Please input a String ..." << endl;
    String str = initString();

    cout << "---------------------Please input the next String ..." << endl;
    String str2 = initString();

    cout << "---------------------The value of the first String is ..." << endl;
    display(str);

    cout << "---------------------The value of the next String is ..." << endl;
    display(str2);

    //test the function of judage a String contain another String or not
    /*bool bRet = contain(str , str2);
    if(bRet) {
        cout << "contain..." << endl;
    } else {
        cout << "not contain..." << endl;
    }*/

    // test the function of insert a String
    /*cout << "---------------------Please input the index where you want to insert into ..." << endl;
    int iInsert;
    cin >> iInsert;
    insert(str , str2 , iInsert);
    cout << "---------------------the String value after insert another String ..." << endl;
    display(str);*/

    //test the function of substance
    /*int iIndex;
    int iLength;
    cout << "---------------------Please input the start index ..." << endl;
    cin >> iIndex;
    cout << "---------------------Please input the length you want to cut ..." << endl;
    cin >> iLength;
    String strSub = subStr(str , iIndex , iLength);
    cout << "---------------------the subString is ..." << endl;
    display(strSub);*/

    //test the compare function
    int iRes = compare(str , str2);
    if(iRes == 1) {
        cout << "bigger" << endl;
    } else if(iRes == -1) {
        cout << "lesser" << endl;
    } else {
        cout << "same" << endl;
    }


    //test copy a String into another String
    /*copyStr(str , str2);  //copy str2 to str1
    cout << "---------------------The value of the copy String is ..." << endl;
    display(str);*/

    //test the function which link two String
    /*link(str , str2);
    cout << "---------------------The value of the merge String is ..." << endl;
    display(str);*/

    return 0;
}

//initial a String
String initString() {
    String str = (String)malloc(sizeof(Node));

    //initial a MAX_SIZE capcity String
    str->data = (char *)malloc(MAX_SIZE * sizeof(char));
    str->length = 0;
    str->maxSize = MAX_SIZE;

    char in;
    cin.get(in);  //use this function can read in a space

    while(in != '\n') {

        /*when the length of String is less than the maxSize
          append the value into the String directly*/
        if(str->length < str->maxSize) {
            *(str->data + str->length) = in;
            str->length++;
            cin.get(in);
            continue;
        }

        /* when the length of String is bigger than the maxSize
           build a new Space and copy the original data into the new Space
           and then append the Value into the String
        */
        if(str->length >= str->maxSize) {
            str->data = (char *)realloc(str->data , (str->maxSize + INCREASE_SIZE) * sizeof(char));
            str->maxSize = str->maxSize + INCREASE_SIZE;
            *(str->data + str->length) = in;
            str->length++;
        }

        cin.get(in);
    }

    return str;
}

//insert a String into another String
void insert(String str , String s , int index) {
    //judge the index is fit
    if(index < 0 || index > str->length) {
        return;
    }

    /*if the length of the String is bigger than the maxSize
      build a new Space and copy the original into the new Space
    */
    if((str->length + s->length) > str->maxSize) {
        str->data = (char *)realloc(str->data , (str->maxSize + s->length) * sizeof(char));
        str->maxSize = str->maxSize + s->length;
    }

    /* move the value after index */
    for(int j = str->length - 1 ; j >= index; j--) {
        *(str->data + j + s->length) = *(str->data + j);
    }

    /* insert the value of child String into the main String */
    for(int i = 0; i < s->length; i++) {
        *(str->data + index + i) = *(s->data + i);
    }

    // update the length of the main String
    str->length = s->length + str->length;
}

//judge a String is contain another String or not
bool contain(String str , String s) {
    bool bReturn = false;

    // if the child String's length bigger than the main String , return false
    if(str->length < s->length) {
        return bReturn;
    }

    //judge the main String ocntain the child String or not
    int i = 0 , j = 0;
    while(i < str->length && j < s->length) {

        //when the current value is not same
        if(*(str->data + i) != *(s->data + j)) {
            i++;

            /* if the index on the child String is not at the first
               udpate the index as the first place
               begin a new compare with the left String value of the main String
             */
            if(j != 0) {
                j = 0;
            }
        } else {
            i++;
            j++;
        }
    }

    if(j == s->length && *(str->data + i - 1) == *(s->data + j - 1)) {
        bReturn = true;
    }

    return bReturn;
}

//substance the String
String subStr(String str , int index , int length) {
    //judge the varible is fit or not
    if(index < 0 || length == 0 || index > str->length) {
        return NULL;
    }

    /* build a new String as the return value of the function */
    String strReturn = (String)malloc(sizeof(Node));
    strReturn->maxSize = length > MAX_SIZE ? length : MAX_SIZE;
    strReturn->data = (char *)malloc(strReturn->maxSize * sizeof(char));
    strReturn->length = 0;

    /* if the length requestd is bigger than the value length after index
       cut the String vlaue from index to the end of the main String as a result
     */
    if(length > str->length - index) {

        for(int i = index; i < str->length; i++) {
            *(strReturn->data + strReturn->length) = *(str->data + i);
            strReturn->length++;
        }
    } else {

        for(int i = 0 ; i < length; i++) {
            *(strReturn->data + strReturn->length) = *(str->data + index + i);
            strReturn->length++;
        }
    }

    return strReturn;
}

//display the String
void display(String str) {

    for(int i = 0; i < str->length; i++) {
        cout << *(str->data + i);
    }
    cout << endl;
}

//compare two String
int compare(String str1, String str2) {

    int index = 0;

    while(index < str1->length && index < str2->length) {

        /* if the current value is equal compare the next vlaue of the two String */
        if(*(str1->data + index) == *(str2->data + index)) {
            index++;
        } else if(*(str1->data + index) < *(str2->data + index)) {
            return -1;
        } else {
            return 1;
        }
    }

    /* if the child String is over and the main String is not return 1 */
    if(index == str1->length && index != str2->length) {
        return -1;
    } else if(index == str2->length && index != str1->length) {
        /* if the main String is over and the child String is not return -1 */
        return 1;
    } else {
        /* else the main String is equals the child String */
        return 0;
    }
}

//copy the String into another String
void copyStr(String str1 , String str2) {

    // if the capcity is abundant copy the child String's value into the main String
    if(str1->maxSize >= str2->length) {
        str1->length = 0;

        for(int i = 0; i < str2->length; i++) {
            *(str1->data + str1->length) = *(str2->data + i);
            str1->length++;
        }
    } else {
        /* if the length of the main String is less than the child String's length
           build a new Space
           with cpcity of the main String's capcity plus the length of the child String
        */
        str1->data = (char *)realloc(str1->data , (str1->maxSize + str2->length) * sizeof(char));
        str1->length = 0;
        str1->maxSize = str1->maxSize + str2->length;

        for(int i = 0; i < str2->length; i++) {
            *(str1->data + str1->length) = *(str2->data + i);
            str1->length++;
        }
    }
}

//link two String
void link(String strB , String strE) {

    /* when the capcity is not abundant
       build a new Space and copy the original value into the new Space
     */
    if(strB->length + strE->length > strB->maxSize) {
        strB->data =
            (char *)realloc(strB->data , (strB->maxSize + strE->length) * sizeof(char));
        strB->maxSize = (strB->maxSize + strE->length);

        /* append the value of the child String to the main String */
        for(int i = 0; i < strE->length; i++) {
            *(strB->data + strB->length) = *(strE->data + i);
            strB->length++;
        }

    } else {

        for(int i = 0; i < strE->length; i++) {
            *(strB->data + strB->length) = *(strE->data + i);
            strB->length++;
        }
    }
}
分享到:
评论

相关推荐

    C语言 不使用strcat函数实现连接两个字符串功能代码

    下面是用C语言实现不使用是strcat 函数实现连接两个字符串的功能。 源代码: #include void constring(char s[],char t[],char q[]); //函数声明 int main(void) { char s[100]; char q[100]; char t[200]; ...

    存储字符串功能实现小程序.vi

    存储字符串程序.vi

    控制台应用程序,接受字符串大于3的字符串并实现一些功能

    编写控制台应用程序,接受长度大于3的字符串,完成以下功能: 1:输出字符串长度 2:输出字符串中第一个出现字母a的位置 3:在字符串的第3个字符后面插入字符串“hello”,输出新字符串. 4:将字符串“hello”替换为...

    C语言实现字符串截取

    这两个函数都属于自定义实现,它们提供了灵活且实用的功能来帮助开发者轻松地从一个字符串中提取所需的子字符串。 ### 函数一:`subStringByIndex` #### 功能描述 `subStringByIndex` 函数通过指定起始索引(`...

    VB拆分字符串,分隔字符串

    虽然不如`Split`函数直接,但也可以通过`Mid`和`InStr`函数组合来实现字符串的拆分。`Mid`函数用于提取字符串的一部分,而`InStr`函数则用于查找子字符串在主字符串中的位置。以下是一个示例: ```vb Dim str As ...

    关于字符串的简单功能实现

    ### 关于字符串的简单功能实现 #### 摘要与关键词 本次课程设计的主要目标是对字符串执行五项基本操作,并使学生能够理解和掌握利用汇编语言处理字符串的技术。这些基本功能包括:字符串输入、在指定位置插入字符...

    字符串操作封装函数

    标题中的"字符串操作封装函数"指的是将常用的字符串处理功能封装成独立的函数,以便于在代码中重复使用,提高代码的复用性和可读性。这样的做法遵循了软件工程中的模块化原则,能够减少代码冗余,提升开发效率。 ...

    八进制数值字符串转ASCII码字符串C#实现

    在IT领域,编程语言如C#常常用于处理各种数据类型和格式的转换,其中包括将八进制数值字符串转换为ASCII码字符串。八进制是一种基于8的计数系统,常用于计算机科学,而ASCII码(美国标准信息交换代码)则是一种用7位...

    用vc实现字符串逆转的功能

    在编程领域,字符串逆转是一个常见的...总的来说,通过VC++实现字符串逆转功能,不仅涉及基本的字符串操作,还包括对C++语言特性的理解和应用,以及对用户交互的理解。这是一个很好的学习C++基础和实践编程技巧的练习。

    [字符串]字符串提取(获取两个字符串中间的字符串)

    在标准的字符串方法中,没有直接提供一个功能可以获取两个已知字符串之间的子串,但我们可以通过结合几个基本方法来实现这个功能。 1. **IndexOf**:此方法用于查找指定字符串在当前字符串中的第一次出现位置。...

    C#字符串删除指定字符串|字符串删除子字符串

    本篇文章将详细探讨如何在C#中实现这一功能,包括多种方法和实用技巧。 首先,C#提供了多种内置方法来操作字符串,比如`Remove`、`Replace`和`Substring`等。当我们想要删除字符串中的指定子串时,我们可以根据具体...

    用指针实现字符串的插入

    在上述代码中,`insert`函数实现了将一个字符串(由`strin`指针指向)插入到另一个字符串(由`str`指针指向)的指定位置的功能。这个过程涉及到了对指针的灵活运用以及对字符串的逐个字符处理,具体步骤如下: 1. *...

    plsql分割字符串

    PL/SQL(Procedural Language for Oracle)作为Oracle数据库的一种强大工具,提供了丰富的功能来处理字符串,包括分割、连接、替换等操作。本文将深入探讨如何在PL/SQL中实现字符串的分割,并通过具体代码示例进行...

    统计字符串中子字符串出现的次数,并返回

    综上所述,"统计字符串中子字符串出现的次数,并返回"这一功能涉及了C#中的字符串基础知识、字符串查找方法、循环与递归逻辑、正则表达式以及算法优化等多个方面。通过深入理解和实践这些知识点,可以更好地应对各种...

    字符串分割的字符串数量 SQL

    本篇文章将详细介绍如何利用SQL语言实现这一功能,具体来说是如何编写一个SQL函数来计算给定字符串中由特定分隔符分隔出的子字符串的数量。 #### 题目背景 在实际应用场景中,有时会遇到存储了多个值的字符串字段...

    C#实现移除字符串末尾指定字符的方法

    总结起来,C#中实现移除字符串末尾指定字符的方法主要涉及到字符串的`LastIndexOf`和`Substring`方法,通过这两个方法的组合,我们可以高效地完成字符串的处理,满足各种应用场景的需求。同时,单元测试对于确保代码...

    C++实现的String字符串类

    在C++编程语言中,`std::string`是用于处理字符串的基本工具,它是一个标准库类型,提供了丰富的功能。然而,有时为了学习目的或者特定需求,我们可能需要自定义一个字符串类,就像这个作业所要求的那样。在这个...

    C语言写字符串函数及任意个数求和

    然而,为了更好地理解和掌握C语言,有时我们需要自己动手实现这些功能,这就是"用C语言重写字符串功能函数"的意义所在。 1. **字符串复制(strncpy or custom function)**: C语言标准库中的`strcpy`函数用于将一...

    字符串计数 C语言实现 ACM习题

    根据给定的信息,本文将对“字符串计数 C语言实现 ACM习题”这一主题进行深入解析,主要包括以下几个方面的内容:题目背景、问题描述、代码分析以及解决方案。 ### 题目背景 本题属于ACM竞赛中的基础练习题,旨在...

    TIA博途-字符转换为字符串以及截取字符串有效字符的具体方法示例.docx

    在处理通信数据时,我们经常需要对字符和字符串进行操作,例如将字符转换为字符串,或者从字符串中截取有效字符。以下将详细介绍在TIA博途中如何实现这些操作。 首先,字符转换为字符串的过程通常涉及到ASCII码。...

Global site tag (gtag.js) - Google Analytics