Data_Structure/作業/期中考/string_fun.cpp
2025-01-20 21:30:53 +08:00

124 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
// 若輸入含有\n的話 使用此函數移除\n
// 將原先\n的位置替換為\0
name[strcspn(name, "\n")] = '\0';
// strlen() - 計算字串長度
char str[] = "Hello";
int length = strlen(str); // 結果為 5
printf("strlen(Hello) = %d\n",length);
// strcpy() - 複製字串
char dest[50];
char src[] = "Hello";
strcpy(dest, src); // dest 現在包含 "Hello"
printf("strcpy(dest, Hello) => dest = %s\n",dest);
// strcat() - 連接字串
char str1[50] = "Hello ";
char str2[] = "World";
strcat(str1, str2); // str1 現在是 "Hello World"
printf("strcat(str1 = Hello, str2 = World) => str1 = %s\n",str1);
// strcmp() - 比較字串
char str3[] = "Hello";
char str4[] = "Hello";
int result = strcmp(str3, str4); // 相同回傳 0str1 大於 str2 回傳正數,小於回傳負數
printf("strcmp(Hello, Hello) = %d\n",result);
// strchr() - 尋找字元在字串中第一次出現的位置
char str5[] = "Hello";
char *p1 = strchr(str5 , 'l'); // 指向第一個 'l' 的位置
printf("strchr(Hello, 'l') = %p\n",*p1);
// strstr() - 尋找子字串在字串中第一次出現的位置
char str6[] = "Hello World HELLO";
char *p2 = strstr(str6, "World"); // 指向 "World" 開始的位置
// char *p3 = strstr(NULL, " ");
printf("strstr(str6, World) = %d\n",p2 - str6 );
// printf("strstr(, HELLO) = %d\n",p3 - str6 );
// strchr函式的實際應用
// 函數原型char *strchr(const char *str, int c)
// 功能:在字串中尋找指定字元第一次出現的位置
// 返回值:
//
// 若找到字元,返回該字元的指針
// 若沒找到,返回 NULL
//
// 注意:第二個參數雖然是 int 類型,但實際上會被轉換成 char 來搜尋
// char str[] = "programming";
// char *p;
// // 尋找字母 'g'
// p = strchr(str, 'g');
// if (p != NULL) {
// printf("找到 'g' 的位置: %ld\n", p - str); // 輸出3
// printf("從該位置到結尾的子字串: %s\n", p); // 輸出gramming
// }
//
// // 尋找所有 'm' 的位置
// char *temp = str;
// while ((temp = strchr(temp, 'm')) != NULL) {
// printf("找到 'm' 的位置: %ld\n", temp - str);
// temp++; // 移到下一個位置繼續搜尋
// }
//
// // 尋找不存在的字元
// p = strchr(str, 'z');
// if (p == NULL) {
// printf("找不到字元 'z'\n");
// }
// strstr() 函數
// 函數原型char *strstr(const char *haystack, const char *needle)
// 功能在主字串haystack中尋找子字串needle第一次出現的位置
// 返回值:
//
// 若找到子字串,返回子字串首次出現位置的指針
// 若沒找到,返回 NULL
//
// 注意:區分大小寫
// char str[] = "This is a simple example of strstr function";
// char *p;
//
// // 基本搜尋
// p = strstr(str, "simple");
// if (p != NULL) {
// printf("找到子字串的位置: %ld\n", p - str); // 輸出10
// printf("從該位置到結尾的文字: %s\n", p); // 輸出simple example of strstr function
// }
//
// // 搜尋重複出現的子字串
// char text[] = "hello hello hello";
// char *temp = text;
// while ((temp = strstr(temp, "hello")) != NULL) {
// printf("找到 'hello' 的位置: %ld\n", temp - text);
// temp++; // 移到下一個位置繼續搜尋
// }
//
// // 搜尋空字串
// p = strstr(str, "");
// if (p != NULL) {
// printf("空字串匹配原字串起始位置\n"); // 會匹配到起始位置
// }
//
// // 區分大小寫的範例
// p = strstr(str, "SIMPLE"); // 不會找到,因為大小寫不同
// if (p == NULL) {
// printf("找不到 'SIMPLE',因為是區分大小寫的\n");
// }
return 0;
}