进击的暑假(二)

最近集中处理了一些关于字符串的题,在这里做一些记录

1.题目:有一个字符数组的内容为:"student a am i",请你将数组的内容改为"i am a student".

要求:不能使用库函数。只能开辟有限个空间(空间个数和字符串的长度无关)。

提示

student a am i

i ma a tneduts

i am a student

解答

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

int my_strlen(const char str[])//编写求字符串数组长度的函数
{
    int count=0;
    while(*str++)
    {
        count++;
    }
    return count;
}

void reverse_str(char *left,char *right)
{
    assert(left);//指针进行有效性判断
    assert(right);
    while(left<right)//字符数组及要提取的字符串进行逆序
    {
        char temp=*left;
        *left=*right;
        *right=temp;
        left++;
        right--;
    }
}

void reverse(char *str)
{
    char *start=str;
    char *end=str+my_strlen(str)-1;
    char *cur=NULL;
    if(str==NULL||str[0]=='\0')//此处有两个判断条件,用或逻辑
    {
        return;
    }
    reverse_str(start,end);//逆序排列字符数组
    while(*str)
    {
        cur=str;
        while(*str!=' '&&*str!='\0')//注意此处条件是两个,包括*arr!=‘\0'
        {
            str++;
        }
        end=str-1;
        reverse_str(cur,end);
        if(*str==' ')str++;//继续让字符指针++
    }
}

int main()
{
    char arr[]="student a am i";
    reverse(arr);
    printf("%s\n",arr);
    system("pause");
    return 0;
}

2.题目:在字符串中删除特定的字符.输入They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”。

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include <cstring>

void Del(char *pStr,const char *pDel)
{
    if(pStr==NULL || pDel ==NULL)
        return ;
    const int size = 256;
    int hash[size];
    memset(hash,0,sizeof(hash));

    const char *pTemp = pDel;
    while(*pTemp!='\0')
        hash[*(pTemp++)]=1;

    char *pSlow = pStr;
    char *pFast = pStr;
    while(*pFast!='\0')
    {
        if(hash[*pFast]!=1)//是正常的字符串,就把它存进去
        {
            *pSlow = *pFast;
            pSlow++;
        }
        pFast++;//否则的话就只读而不存进去
    }
    //  最后不要忘记加'\0'结束符,在原地修改了原字符串
    *pSlow = '\0';
    printf("%s\n",pStr);
}

int main()
{
    char pstr[] = "They are students";
    char pdstr[] = "aeiou";
    Del(pstr,pdstr);
    //printf("%s\n",pStr);
    return 0;
}

3.题目:实现一个函数,可以左旋字符串中的k个字符。ABCD左旋一个字符得到BCDA,ABCD左旋两个字符得到CDAB

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Left(char c[], int len , int k)
{
    k = k%len;
    char *p = (char *)malloc((2*len+1)*sizeof(char));
    strcpy(p, c);
    strcat(p, c);//"ABCDEABCDE"
    strncpy(c, p+k, len);//从下标2开始,取5个数,拷贝到c中
    free(p);
}
int main()
{
    char c[] = "ABCDE";
    int k = 2;
    int len = strlen(c);
    printf("左旋前:%s\n", c);
    Left(c,len,k);
    printf("左旋后:%s\n", c);
    return 0;
}

4.题目:判断一个字符串是否为另外一个字符串旋转之后的字符串。

例如:给定s1 =AABCD和s2 = BCDAA,返回1给定s1=abcd和s2=ACBD,返回0.

AABCD左旋一个字符得到ABCDA

AABCD左旋两个字符得到BCDAA

AABCD右旋一个字符得到DAABC

#include <stdio.h>
#include <windows.h>
#include <string.h>
int judge(char str1[],char str2[])
{
    int i = 0;
    int j = 0;
    int lenght = strlen(str1);
    for (j = 1; j <= lenght; j++)
    {
        {
            int tem = str1[0];
            for (i = 0; i < lenght - 1; i++)
            {
                str1[i] = str1[i + 1];
            }
            str1[lenght - 1] = tem;
        }

        if (0 == strcmp(str1, str2))
        {
            return 1;
        }
    }
    return 0;
}
int main()
{
    char s1[10] = "ABCDEF";
    char s2[10] = "EFABCD";
    printf("s1 = %s\n", s1);
    printf("s2 = %s\n", s2);

    printf("%d\n", judge(s1, s2));

    system("pause");
    return 0;
}

5.第一个只出现一次的字符,例如abcaccd 输出b

#include <iostream>
using namespace std;
int getFirstNoRepeatCh()
{
    char str[256];
    int s[256],i;
    cout<<"please input the word:";
    cin>>str;
    for(i=0;i<256;i++)
        s[i]=0;
    for(i=0;str[i]!='\0';i++)
        s[str[i]]++;
    for(i=0;str[i]!='\0';i++)
    {
        if(s[str[i]]==1)
        {
            cout<<str[i]<<endl;
            return i;
        }
    }
    return 0;
}

int main ()
{
    getFirstNoRepeatCh();
}

6.

写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)

功能:
在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

unsigned int Continumax(char** pOutputstr,  const char* intputstr)//为啥要传一个二级指针进去?
{
    bool isDigiit = false;  //字符串中是否有数字
    int i = 0, maxLength = 0, beginIndex = 0;
    int currLength = 0,currIndex = 0;

    /* 遍历输入的字符串 */
    for (i = 0; intputstr[i] != '\0'; i++)
    {
        /* 找到是数字的字符 */
        if (intputstr[i] >= '0' && intputstr[i] <= '9')
        {
            currLength = 0;
            isDigiit = true;
            currIndex = i;
            while (intputstr[i] != '\0' && (intputstr[i] >= '0' && intputstr[i] <= '9'))
            {
                i++;
                currLength++;
            }

            if (currLength >= maxLength)
            {
                maxLength = currLength;
                beginIndex = currIndex;
            }
        }
    }

    /* 如果字符串中没有数字 */
    if (isDigiit == false)
    {
        *pOutputstr = "";
    }
    else
    {
        (*pOutputstr) = (char*)malloc(maxLength + 1);
        memset((*pOutputstr), 0, maxLength + 1);
        strncpy(*pOutputstr, intputstr + beginIndex, maxLength);//把位置记住,直接调用字符串函数就可以了
        (*pOutputstr)[maxLength] = '\0';
    }

    //return maxLength;
    printf("%d\n",maxLength);
    printf("%s\n",*pOutputstr);
}
int main()
{
    const char *instr ="abcd12345ed125ss123456789";
    char outstr [] = "";
    Continumax(reinterpret_cast<char **>(outstr),  instr);

}

7.把一个字符常量,放进一个字符数组

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
    const char *p ="abcdef";
    char q[10];
    int i = 0;
    while (*p != '\0')
    {
        //(*q) = *p;
        q[i++] = *p;
        ++p;
    }
    q[i] = '\0';
    //printf("%s\n", q);
    cout<<q<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值