一、c语言sqrt函数的用法
1、sqrt函数用于求一个数的平方根。
2、函数原型:double sqrt(double x);
3、x为待求平方根的数。
#include <stdio.h>
#include <math.h>
int main() {
double num = 64.0;
double res;
res = sqrt(num);
printf("Square root of %.2lf is %.2lf", num, res);
return 0;
}二、c语言strcmp函数用法
1、strcmp函数用于比较两个字符串是否相等。
2、函数原型:int strcmp(const char* str1, const char* str2);
3、str1为第一个字符串,str2为第二个字符串,函数返回值为0时表示两个字符串相等。
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
int res = strcmp(str1, str2);
printf("String comparison result: %d", res);
return 0;
}三、c语言strcat函数用法
1、strcat函数用于将两个字符串拼接在一起。
2、函数原型:char* strcat(char* dest, const char* src);
3、dest为目标字符串,src为要拼接的字符串,函数返回拼接后的结果。
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
strcat(str1, str2);
printf("Result string: %s", str1);
return 0;
}四、c语言中strstr函数的用法
1、strstr函数用于在一个字符串中查找另一个子字符串。
2、函数原型:char* strstr(const char* str1, const char* str2);
3、str1为要查找的字符串,str2为要查找的子字符串,函数返回查找到的子字符串在目标字符串上的首地址。
五、strstr函数的返回值
1、如果找到了子字符串,返回子字符串在目标字符串上的首地址。
2、如果未找到子字符串,返回NULL。
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello World";
char str2[20] = "lo";
char* res = strstr(str1, str2);
printf("Result string: %s", res);
return 0;
}六、strstr函数的用法例子
1、判断一个字符串中是否包含子字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello World";
char str2[20] = "lo";
if (strstr(str1, str2) != NULL) {
printf("Substring found");
}
else {
printf("Substring not found");
}
return 0;
}2、替换一个字符串中的子字符串。
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello World";
char str2[20] = "lo";
char newStr[20] = "asdfghjkl";
char* res = strstr(str1, str2);
if (res != NULL) {
strncpy(res, newStr, strlen(newStr));
}
printf("Result string: %s", str1);
return 0;
}七、strstr函数的作用是
1、在一个字符串中查找另一个子字符串。
2、判断一个字符串中是否包含指定的子字符串。
3、可以用于替换一个字符串中的子字符串。
八、strstr函数c语言实现
char* strstr(const char* str1, const char* str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
for (int i = 0; i <= len1 - len2; i++) {
int j;
for (j = 0; j < len2; j++) {
if (str1[i+j] != str2[j]) {
break;
}
}
if (j == len2) {
return (char*)&str1[i];
}
}
return NULL;
}九、实现strstr函数
char* my_strstr(const char* str1, const char* str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
for (int i = 0; i <= len1 - len2; i++) {
int j;
for (j = 0; j < len2; j++) {
if (str1[i+j] != str2[j]) {
break;
}
}
if (j == len2) {
return (char*)&str1[i];
}
}
return NULL;
}十、c语言typedef struct用法
1、typedef struct用法可以定义一个新的数据类型。
2、struct类型在定义的时候需要写出完整的struct语句,而用typedef定义的类型则可以用简单的类型名来代替。
3、示例代码如下:
#include <stdio.h>
typedef struct {
char name[20];
int age;
} Person;
int main() {
Person p;
strcpy(p.name, "Tom");
p.age = 30;
printf("Person name: %s\n", p.name);
printf("Person age: %d", p.age);
return 0;
}原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/185583.html
微信扫一扫
支付宝扫一扫