三个 编程题 :1. 回文 2. 将字符串t连接到字符串s的尾部

/*
编写一个函数,判断某个字符串是否为回文。
回文就是从左边开始读 和 从右边开始读 都是一样的,比如"abcba"
*/

#include <string.h>
#include <stdio.h>
int isHuiwen(char *str);

int main()
{
    printf("%d\n", isHuiwen("a"));
    return 0;
}

/*
 返回1代表是回文
 返回0代表不是回文
 */
int isHuiwen(char *str)
{
    // 1.定义一个指向变量left指向字符串的首字符
    char *left = str;
    // 2.定义一个指向变量right指向字符串的末字符
    char *right = str + strlen(str) - 1;
    
    while (left < right)
    {
        // 如果左边和右边的字符不一样
        if (*left++ != *right--)
        {
            return 0;
        }
    }
    
    return 1;
}

/*
编写一个函数void strlink(char s[], char t[])
 将字符串t连接到字符串s的尾部
*/
#include <stdio.h>

void strlink(char s[], char t[]);

int main()
{
    char s1[20] = "michael ";
    char s2[] = "jackson";
    
    strlink(s1, s2);
    
    printf("%s\n", s1);
    
    return 0;
}

void strlink(char s[], char t[])
{
    int i = 0;
    
    // 判断s[i]是否为字符串的尾部
    while ( s[i] != '\0' )
    {
        i++;
    }
    
    int j = 0;
    // 拷贝t的内容到s的后面
    while ( (s[i] = t[j]) != '\0' )
    {
        i++;
        j++;
    }
}

/*
 更加精简的写法,仅作为参考(会有警告信息)
void strlink2(char s[], char t[])
{
    int i = -1;
    
    // 判断s[i]是否为字符串的尾部
    while (s[++i]) ;
    
    int j = 0;
    // 拷贝t的内容到s的后面
    while (s[i++] = t[j++]) ;
}*/

/*
编写一个函数void strlink(char *s, char *t)
 将字符串t连接到字符串s的尾部
*/
#include <stdio.h>

void strlink(char *s, char *t);

int main()
{
    char s1[20] = "michael ";
    char s2[] = "jackson";
    
    strlink2(s1, s2);
    
    printf("%s\n", s1);
    
    return 0;
}

void strlink(char *s, char *t)
{
    // 判断s[i]是否为字符串的尾部
    while ( *s != '\0' )
    {
        s++;
    }
    
    // 拷贝t的内容到s的后面
    while ( (*s = *t) != '\0' )
    {
        s++;
        t++;
    }
}



/*
 更加精简的写法,仅作为参考(会有警告信息)
 void strlink2(char *s, char *t)
 {
     // 判断s[i]是否为字符串的尾部
     while (*s++);
     
     // 减回一次
     s--;
     
     // 拷贝t的内容到s的后面
     while ( *s++ = *t++ ) ;
 }*/

  

转载于:https://www.cnblogs.com/sunyao/p/3754890.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值