C++學習筆記(7)

#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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章