嵌入式学习-01C语言day09

  1. 定义函数 返回某个字符在字符串中第一次出现的位置
#include <stdio.h>
/**
 * 1. 定义函数 返回某个字符在字符串中第一次出现的位置
*/

int num(char *, char );

int main(){

    char a[10];
    char b;
    printf("请输入字符串和字符");
    scanf("%s %c", a, &b);
    printf("首次出现位置为:%d", num(a, b));

    return 0;
}


int num(char *arr, char c)
{
    int n = 0;
    while ( *arr != '\0')
    {
        if (*arr == c)
        {
          break;  
        }
        n++;
        *arr++;        
    }
    return n;
    
}
  1. 定义函数 返回某个字符在字符串中最后一次出现的位置
#include <stdio.h>
#include <string.h>
/**
 * 2. 定义函数 返回某个字符在字符串中最后一次出现的位置  int lastIndex(char *, char)
 */
int lastIndex(char *, char);
int main()
{
    char a[10];
    char b;
    printf("请输入字符串和字符");
    scanf("%s %c", a, &b);
    printf("最后出现位置为:%d", lastIndex(a, b));
    return 0;
}

int lastIndex(char *arr, char c)
{
    int n = 0;
    int s = strlen(arr);
    for (int i = 0; i < s; i++)
    {
        if (arr[i] == c)
        {
            n = i;
        }
    }

    return n;
}

  1. 定义函数 实现字符串中小写字母转大写字母;
#include <stdio.h>
/***
 * 3. 定义函数 实现字符串中小写字母转大写字母; 	char *upperCase(char *)
 */

void *upperCase(char *);

int main()
{
    char aa[10];
    printf("请输入字符串");
    scanf("%s", aa);

    upperCase(aa);
    printf("%s", aa);
    return 0;
}

void *upperCase(char *s)
{
    while (*s != '\0')
    {
        if (*s >= 'a' && *s <= 'z')
        {
            *s -= 32;
        }
        *s++;
    }
}

  1. 定义函数 实现字符串中大写字母转小写字母;
#include <stdio.h>
/**
 * 4. 定义函数 实现字符串中大写字母转小写字母;    char *lowerCase(char *)
 */
void *lowerCase(char *);
int main()
{
    char aa[10];
    printf("请输入字符串");
    scanf("%s", aa);
    lowerCase(aa);
    printf("%s", aa);
    return 0;
    return 0;
}
void *lowerCase(char *s)
{
    while (*s != '\0')
    {
        if (*s >= 'A' && *s <= 'Z')
        {
            *s += 32;
        }
        *s++;
    }
}

  1. 定义函数,实现交换两个变量的值; 函数原型
#include <stdio.h>
/***
 * 5. 定义函数,实现交换两个变量的值; 函数原型 void exchange(int *, int *)
 */
void exchange(int *, int *);
int main()
{
    int n1, n2;
    printf("请输入两个整数: ");
    scanf("%d %d", &n1, &n2);
    exchange(&n1, &n2);
    printf("%d  %d", n1, n2);
    return 0;
}
void exchange(int *a, int *b)
{
    int c;
    c = *a;
    *a = *b;
    *b = c;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路x飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值