一、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/zh-hk/n/185583.html
微信掃一掃
支付寶掃一掃