C/C++试题集——字符串篇

本来这一篇应该是C/C++试题集——链表篇的,但是做了一下题目,对于链表小鱼还不是很熟悉,所以还是先看知识点,再做题。过两天再更新链表篇试题集。

对于数组而言,首地址和长度最重要,所以要实现对数组的操作必须制定这两个元素。
对于c风格字符串而言,不需要指定长度,因为c风格字符串他指定了结束符。
字符串:字符串是指一串字符,元素类型必须是char类型,有效的字符串应该以0结尾。
字符串长度(strlen)是指:从第一个字符开始,到结束符0,中间字符的个数。
字符串的操作主要分为:字符串的遍历;字符串的长度计算(0之前的字符个数);字符串的复制(会把0一起拷贝,指字符串中每一个字节的复制。需要注意:目标缓冲区要足够大,避免越界);字符串的比较(字符串中每个字符相同,这两字符串才相同);字符的删除(单个字符删除及多个字符删除);中间字符的插入(单个字符可以选择从后往前插入,多字符考虑复制代替插入);字符串的分割(调整尾部和调整头部两方法)。

1、字符串的遍历:

相关试题:
A、输入一个字符串,然后将每一个字符打印显示,中间加一个空格。

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

void print_string(const char* str)
{
    int len = strlen(str);
    for (int i = 0; i < len; i++)
    {
        if (str[i] == 0)
            break;
        printf("%c ", str[i]);
    }
}

int main()
{
    const char* a = "abcd";
    print_string(a);
    return 0;
}

B、进行字符串遍历,实现字符串格式替换
描述:
处理一个字符串(仅英文字符),将里面的特殊符号转义为表情。
/s 转为 ^_^
/f 转为 @_@
/c 转为 T_T
法一:

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

void translate(const char* str)
{
    const char* p = str;
    while (*p)
    {
        char ch = *p;
        if (*p != '/')
        {
            printf("%c", ch);
        }
        else
        {
            p++;
            ch = *p;
            if (ch=='s')
            {
                printf("^_^");
            }
            else if (ch == 'f')
            {
                printf("@_@");
            }
            else if (ch =='c')
            {
                printf("T_T");
            }
            else
            {
                printf("/%c", ch);
            }
        }
        p++;
    }

}

int main()
{
    const char* a = "Thank you/s I will try my best/c";
    translate(a);
    return 0;
}

法二:

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

void translate(const char* str)
{
    int len = strlen(str);
    for (int i = 0; i < len; i++)
    {
        if (str[i] == '/'&& str[i+1] == 's')
        {
            printf("^_^");
            i++;
        }
        else if (str[i]=='/'&&str[i+1] =='c')
        {
            printf("T_T");
            i++;
        }
        else if (str[i]=='/'&& str[i+1] == 'f')
        {
            printf("@_@");
            i++;
        }
        else
        {
            printf("%c", str[i]);
        }
    }

}

int main()
{
    const char* a = "Thank you/s I will try my best/c";
    translate(a);
    return 0;
}

2、字符串的比较:

相关试题:
A:回答检查。例如,有一个系统需要您回答问题。
系统显示: “你生日的月份是?”
如果回答是 “1” 或”jan”或”january”是算正确,显示”您输入正确”
除此之外算错误,显示”您输入错误”

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


void compare(const char* str)
{
    //调用字符串比较函数,strcmp,当两字符串相同时,值为0。
    if (strcmp(str,"jan")==0
        ||strcmp(str,"january") ==0
        ||strcmp(str,"1") == 0)
    {
        printf("你输入正确");
    }
    else
    {
        printf("你输入错误");
    }
}
char get_input()
{
    char a[16];
    printf("你生日的月份是?\n");
    gets_s(a);
    compare(a);
    return 0;
}
int main()
{
    get_input();
    return 0;
}

B:输入5字符串,将它们排序后输出。
描述:
例如,输入了5个字符串: “dead”, “alex”, “can”, “best”, “effort”
则程序最终输出为: alex, best, can, dead, effort

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

struct Object
{
    char value[32];
};

void select_sort(char* arr[], int n)
{
    for (int i = 0; i < n-1; i++)
    {
        for (int j = i+1; j < n; j++)
        {
            if (strcmp(arr[j], arr[i]) < 0)
            {
                char* temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
            }
        }
    }
}

int main()
{
    int i;
    Object objs[5];
    for (i = 0; i < 5; i++)
    {
        printf("please input:");
        gets_s(objs[i].value);
    }
    printf("over input.\n");
    char* name[5];
    for (i = 0; i < 5; i++)
    {
        name[i] = objs[i].value;
    }
    select_sort(name, 5);
    for (i = 0; i < 5; i++)
    {
        printf("%s \n", name[i]);
    }
    return 0;
}

3、字符串换格式复制

相关试题:
A:有一个整数数组,要求将其格式化为字符串,每个数字以逗号分开。
完成函数:
void to_string(const int* arr, int size, char* output)
{
}
例如,
int arr[4] = { 18, 987, 1235, -911 };
char buf[512];
to_string(arr, 4, buf);
则buf被格式化为字符串 “18, 987, 1235, -911”

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


void to_string(const int* arr,int size,char* output)
{
    char buf[128];
    int count = 0;
    for (int i = 0; i < size; i++)
    {
        sprintf_s(buf, "%d,", arr[i]);
        for (int j = 0; j < buf[j]; j++)
        {
            output[count] = buf[j];
            count++;
        }
    }
    output[count] = 0;
}

int main()
{
    int arr[4] = { 18,987,1235,-911 };
    char buf[512];
    to_string(arr, 4, buf);
    int len = strlen(buf);
    for (int i = 0; i < len; i++)
    {
        printf("%c", buf[i]);
    }
    printf("\n");
    return 0;
}

4、字符串的删除(删除字符相当于后面所有的字符都要前移,cpu成本太高,可以从后往前查找再删除;当删除字符太多的情况下,最佳的方案是复制一份)
复制介绍:
首先申请一个相同大小的内存,eg:len = strlen(str); char* copy = (char*)malloc(len+1);再进行边搜索边复制

相关试题:
A、去除字符串两端的空格
例如,
用户输入 ” i am fine ”
则有效的输入是 “i am fine”

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

char* AfTrim(char* src)
{
    int len = strlen(src);
    int start = 0;  //copy字符串的开头引号
    int end = 0;  // copy字符串的结尾引号
    int i = 0;  //从头遍历的引号
    int k = len-1;  // 从尾遍历的引号
    char* copy = (char*)malloc(len + 1); //申请一片相同大小的内存
    while (src[i])  //从头遍历,直至找到不为空格终止
    {
        if (src[i] == ' ')
            i++;
        else
        {
            start = i;
            break;
        }
    }
    while (src[k])  //从尾遍历,直至找到不为空格终止
    {
        if (src[k] == ' ')
        {
            k--;
        }
        else
        {
            end = k+1;
            break;
        }
    }
    //进行copy
    for (int j = start; j < end; j++)
    {
        copy[j-start] = src[j];
        printf("%c", src[j]);
    }
    return copy;
    free(copy);
}

int main()
{
    char a[] = "   i am fine   ";
    AfTrim(a);
    return 0;
}

B:将阿拉伯数字转成中文数字,例如,输入字符串”我爱12你好34”,输出”我爱一二你好三四”(知识点:汉字及数字的替换)

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

char* Change(char* src)
{
    int len = strlen(src);
    int count = 0;
    char* copy = (char*)malloc(len + 1);
    char* chinese[10] = { "零","一","二","三","四","五","六","七","八","九"}; //定义一个中文数字数组
    for (int i = 0; i < len; i++)
    {
        if (src[i] == '\0') //终止符就结束
            break;
        if (src[i]>='0'&&src[i]<='9') //搜索到数字字符时
        {
            int num = src[i] - '0';   //得到数字对应的序号
            char* hanzi = chinese[num];//去除该序号对应的数组元素
            copy[count] = hanzi[0];//因为一个中文字符占两个字节,所以要分开赋值
            copy[count + 1] = hanzi[1];
            count+=2;
        }
        else
        {
            copy[count] = src[i];//其他的都不管,照常copy
            count++;
        }
    }
    for (int j = 0; j < count; j++)
    {
        printf("%c", copy[j]);
    }
    printf("\n");

    return copy;
    free(copy);
}

int main()
{
    char a[] = "我爱12你好34";
    Change(a);
    return 0;
}

5、字符串的分割

相关试题:
A:字符串分解:一段文本以逗号分开,写一个函数将各段文本打印出来
描述:
一段文本以逗号分开,写一个函数将各段文本打印出来。
例如,文本为”hello, world , good, morning “,
则输出”hello”, “world”,”good”, “morning”

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

void Split(const char* text)
{
    int len = strlen(text);
    char* copy = (char*)malloc(len + 1);
    //当搜索到逗号时,将逗号变成终结符
    for (int i = 0; i < len; i++)
    {
        if (text[i] == '\0')
            break;
        if (text[i] == ',')
        {
            copy[i] = '\0';
        }
        else
        {
            copy[i] = text[i];
        }
    }
    //进行分解输出
    for (int j = 0; j < len; j++)
    {
        if (copy[j] == '\0')
        {
            printf("\n");
        }
        else
        {
            printf("%c", copy[j]);
        }
    }

    free(copy);
}

int main()
{
    char a[] = "hello,world,good,morning";
    Split(a);
    return 0;
}

B:用户输入一个字符串,其中包括若干数字,要求将数字提取出来。
描述:
例如,提示及用户输入如下:
请输入数据, 以空格分开: 12 15 98 (回车)
注:用户输入时中间可能是一个空格,也可能是多个空格,不影响结果。

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

int parse(const char* input, int numbers[])
{
    int count = 0;
    //char buf[128];
    int start, end;
    int flag = 0; //字符是否有效,为1则有效,为0则无效
    int len = strlen(input);
    for (int i = 0; i < len; i++ )
    {
        if (input[i] == '\0')
            break;
        else if (input[i]>='0'&&input[i]<='9')
        {
            if (flag == 0)
            {
                start = i;
                flag = 1;
            }   
        }
        else
        {
            if (flag ==1)
            {
                end = i;
                flag = 0;
                count++;
                for (int j = start; j < end; j++)
                {
                    printf("%c", input[j]);
                }
                printf("\n");
            }
        }
    }

    printf("%d\n", count);
    return count;
}

int main()
{
    int number[128];
    char* str = { "12    15 98 " };
    parse(str, number);

    return 0;
}

C:字符串的替换
描述:
完成函数:
int replace( char* src, const char* subtext, const char* newtext);
完成对源字符串src中子串的替换,将subtext出现的地方替换为 newtext
例如,
src: ” just do it . never give up. ”
subtext: “just”
newtext: “some”
注:源字符串中该子串可能出现多次,需依次替换。

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

int replace(char* src, const char* subtext, const char* newtext)
{
    int len1 = strlen(src);
    int len2 = strlen(subtext);
    int j = 0;
    int start = 0;
    for (int i = 0; i < len1; i++)
    {
        if (src[i] == '\0')
            break;
        if (src[i] = subtext[j])
        {
            j++;
        }


    }
    return 0;
}

int main()
{
    char* src = "just do it . never give up. ";
    char* subtext = "just";
    char* newtext = "some";
    replace(src, subtext, newtext);

    return 0;
}

D:字符串提取子串
描述:
要求一个函数,用于提取一个字符串的子串。
例如,对于一个字符串”hello”,可以调用函数substr,通过指定子串的位置和长度信息,得到子串”el“

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

int substr(char* src, int start, int num)
{
    char buf[128];
    for (int i = start; i < start+num; i++)
    {
        buf[i - start] = src[i];
        printf("%c", src[i]);
    }
    buf[num] = 0;
    return 0;
}

int main()
{
    char* src = "hell0";
    substr(src,1, 2);

    return 0;
}

字符串好多扩充点,小鱼觉得好难,这就当一个引导篇,大家一起加油,如果喜欢请留个赞再走哦,持续更新中。。。

  • 16
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值