C++学习笔记(7)

本文深入探讨了使用C语言进行字符串操作的各种技巧,包括遍历、复制和格式化字符串的方法。通过实例展示了如何利用标准库函数如strlen(), strcpy() 和 sprintf() 来高效地处理字符串。此外,还介绍了如何使用C++进行字符串的分割处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

int main41()
{
    //遍历字符串
    const char* p = "wwesdsdasd";
    int len = strlen(p);
    printf("len=%d\n", len);
    for (int i = 0; i < len; ++i)
    {
        printf("%c\n", *p++);
    }
}
int main42()
{
    //遍历字符串
    const char* p = "wwesdsdasd";
    while (*p)
    {
        printf("%c", *p++);
    }
}
int main43()
{
    //复制字符串
    char p[] = "wqwerty";
    char str[100] = {0};
    strcpy(str, p);
    printf("%s", str);
    return 0;
}
int main44()
{
    //格式化
    char ch[100];
    sprintf(ch, "hellozous");
    printf("%s\n", ch);
    sprintf(ch, "%d+%d=%d", 1, 2, 3);
    printf("%s\n", ch);
}
#include <iostream>

using namespace std;

int main45()
{
    //分割
    char s[] = "www.baidu.com";
    const char* delim = ".";
    char* p = strtok(s, delim);
    while (p)
    {
        cout << p << endl;
        p = strtok(nullptr, delim);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值