前言:作为一个安卓程序员,如果不懂C/C++开发,那么安卓jni、NDK、视频解码、音频解码也没法开发,有需求我们就要学习,加油吧!今天开始我们快速讲解C语言,有Java基础的人适合看本博客。
-------------分割线----------
本篇博客代码所需要引用的头文件:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
------------分割线---------
1.使用字符数组储存字符串:
void main(){
//char str[] = {'H','e','l','l','o','\0'};
//char str[6] = { 'H', 'e', 'l', 'l', 'o' };
char str[10] = "Hello";
//可以修改
str[0] = 'W';
printf("%s\n",str);
printf("%#x\n",str);
getchar();
}
2.字符指针:
void main(){
//内存连续排列
char *str = "Hello World";
//不可以修改
//str += 1;
//*str = 'p';
printf("%s\n",str);
printf("%#x\n",str);
//使用指针加法,截取字符串
str += 3;
while (*str)
{
printf("%c",*str);
str++;
}
printf("\n结束");
getchar();
}
3.strcat字符串拼接函数:
void main(){
char dest[50];
char *a = "china";
char *b = " is best";
strcpy(dest,a);
strcat(dest,b);
printf("%s\n",dest);
getchar();
}
4.strchr在一个串中查找给定字符的第一个匹配之处:
void main(){
char *str = "Hello world";
printf("%#x\n",str);
char* p = strchr(str,'w');
if (p)
{
printf("索引位置:%d\n",p-str);
}
else
{
printf("没有找到");
}
getchar();
}
5.strstr 从字符串haystack中寻找needle第一次出现的位置:
void main(void){
char *haystack = "I want go to USA!";
char *needle = "to";
//U元素的指针
char* p = strstr(haystack, needle);
if (p){
printf("索引位置:%d\n", p - haystack);
}
else{
printf("没有找到");
}
system("pause");
}
6.strcmp 比较字符串 strcmpi 比较字符串,忽略大小写:
void main(void){
char *str1 = "abc";
char *str2 = "ABC";
//int r = strcmpi(str1, str2);
int r = _strcmpi(str1, str2);
printf("%d\n", r);
//str1 > str2
if (r > 0){
printf("str1 大于str2\n");
}
else if (r == 0){
printf("str1 等于str2\n");
}
//str1 < str2
else if (r < 0){
printf("str1 小于str2\n");
}
system("pause");
}
7.strset 把字符串s中的所有字符都设置成字符c:
void main(void){
char str[] = "internet change the world!";
_strset(str, 'w');
printf("%s\n", str);
system("pause");
}
8.strrev 把字符串s的所有字符的顺序颠倒过来:
void main(void){
char str[] = "internet change the world!";
_strrev(str);
printf("%s\n", str);
system("pause");
}
9.atoi 字符串转为int类型,atol()将字符串转换为长整型值:
void main(void){
//char* str = "a78";
char* str = "78";
int r = atoi(str);
printf("%d\n", r);
system("pause");
}
10.字符串转为double类型:
void main(void){
char* str = "77b8b";
char** p = NULL;
//char* p = str + 2;
//参数说明:str为要转换的字符串,endstr 为第一个不能转换的字符的指针
double r = strtod(str, p);
printf("%lf\n", r);
printf("%#x\n", p);
system("pause");
}
11.strupr转换为大写:
void main(void){
char str[] = "CHINA motherland!";
_strupr(str);
printf("%s\n", str);
system("pause");
}
12.转换为小写:
void mystrlwr(char str[], int len){
int i = 0;
for (; i < len; i++){
//A-Z 字母 a-Z
if (str[i] >= 'A' && str[i] <= 'Z'){
str[i] = str[i] - 'A' + 'a';
}
}
}
void main(void){
char str[] = "CHINA motherland!";
mystrlwr(str, strlen(str));
printf("%s\n", str);
system("pause");
}
13.删除字符串中指定的字符:
void delchar(char *str, char del){
char *p = str;
while (*str != '\0') {
if (*str != del) {
*p++ = *str;
}
str++;
}
*p = '\0';
}
//删除最后一个字符
int main()
{
char str[] = "vencent ppqq";
delchar(str, 't');
printf("%s\n", str);
system("pause");
}
14.memcpy 由src所指内存区域复制count个字节到dest所指内存区域:
void main(void){
char src[] = "C,C++,Java";
char dest[20] = { 0 };
//字节
memcpy(dest, src, 5);
printf("%s\n", dest);
system("pause");
}
15.memchr 从buf所指内存区域的前count个字节查找字符ch:
void main(void){
char src[] = "C,C++,Java";
char ch = 'a';
//字节 (分段截取)
char* p = memchr(src + 3, ch, 5);
if (p){
printf("索引:%d\n", p - src);
}
else{
printf("找不到\n");
}
system("pause");
}
16.memmove 由src所指内存区域复制count个字节到dest所指内存区域:
void main(){
char s[] = "Michael Jackson!";
//截取的效果
memmove(s, s + 8, strlen(s) - 8 - 1);
s[strlen(s) - 8] = 0;
printf("%s\n", s);
getchar();
}
17.在字符串s1中寻找字符串s2中任何一个字符相匹配的第一个字符的位置,空字符NULL不包括在内:
void main(){
char *s1 = "Welcome To Beijing";
char *s2 = "to";
char *p;
p = strpbrk(s1, s2);
if (p)
printf("%s\n", p);
else
printf("Not Found!\n");
p = strpbrk(s1, "Da");
if (p)
printf("%s", p);
else
printf("Not Found!");
getchar();
}
--------------完--------------