Chèn Chuỗi S1 Vào Giữa S2 - Gists · GitHub
Có thể bạn quan tâm
Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }}
baonq-me/main.c Created November 15, 2018 07:14 Show Gist options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.
Instantly share code, notes, and snippets.
- Download ZIP
- Star (0) You must be signed in to star a gist
- Fork (0) You must be signed in to fork a gist
- Embed Select an option
- Embed Embed this gist in your website.
- Share Copy sharable link for this gist.
- Clone via HTTPS Clone using the web URL.
No results found
Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/baonq-me/51fb638665dca442a6d04431e9fb0193.js"></script> - Save baonq-me/51fb638665dca442a6d04431e9fb0193 to your computer and use it in GitHub Desktop.
- Embed Embed this gist in your website.
- Share Copy sharable link for this gist.
- Clone via HTTPS Clone using the web URL.
No results found
Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/baonq-me/51fb638665dca442a6d04431e9fb0193.js"></script> Save baonq-me/51fb638665dca442a6d04431e9fb0193 to your computer and use it in GitHub Desktop. Download ZIP Chèn chuỗi s1 vào giữa s2 Raw main.c This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters| #include <stdio.h> // Sử dụng các hàm printf(), scanf() |
| #include <stdlib.h> // Sử dụng malloc() |
| #include <string.h> // Sử dụng strlen(), strncpy |
| // Copy n kí tự từ s2 và chèn vào sau s1 |
| void stringCopy(char* s1, char* s2, int n) |
| { |
| // Ngắt nếu input không hợp lệ |
| if (s1 == NULL || s2 == NULL || n == 0) |
| { |
| return; |
| } |
| // Chỉ số của null terminated character trong s1 |
| // (là vị trí sẽ chèn s2 vào) |
| int pNextChar = strlen(s1); // s[pNextChar] = '\0' |
| // Copy s2 vào s1 |
| for (int i = 0; i < n; i++) |
| { |
| s1[pNextChar++] = s2[i]; |
| } |
| s1[pNextChar] = '\0'; |
| } |
| // Chèn s1 vào giữa s2 |
| // Có thể sử dụng hàm mặc định của c là strncpy() thay cho stringCopy tự viết |
| // https://www.tutorialspoint.com/c_standard_library/c_function_strncpy.htm |
| char* insert(char* s1, char* s2) |
| { |
| // Ngắt nếu input không hợp lệ |
| if (s1 == NULL || s2 == NULL) |
| { |
| return NULL; |
| } |
| int sizeS1 = strlen(s1); |
| int sizeS2 = strlen(s2); |
| // Cấp phát bộ nhớ để lưu chuỗi s |
| char* s = (char*)malloc(sizeS1 + sizeS2 + 1); |
| //char* s = (char*)calloc(sizeS1 + sizeS2 + 1, sizeof(char)); |
| // Chỉ số của kí tự nằm giữa xâu s2 mà tại đó ta sẽ cắt s2 |
| int pMidS2 = (sizeS2 + 1) / 2; |
| // Copy nửa đầu của s2 vào s |
| stringCopy(s, s2, pMidS2); |
| //strncpy(s, s2, pMidS2); |
| // Copy s1 vào s |
| stringCopy(s, s1, sizeS1); |
| //strncpy(s + pMidS2, s1, sizeS1); |
| // Copy nửa sau của s2 vào s |
| stringCopy(s, s2 + pMidS2, sizeS2 - pMidS2); |
| //strncpy(s + pMidS2 + sizeS1, s2 + pMidS2, sizeS2 - pMidS2); |
| // %lu là format của kiểu unsigned long |
| printf("strlen(s) = %lu\n", strlen(s)); |
| return s; |
| } |
| int main() { |
| char s1[100] = ""; |
| char s2[100] = ""; |
| printf("Nhap vao xau s1: "); |
| scanf("%s", s1); |
| printf("Nhap vao xau s2: "); |
| scanf("%s", s2); |
| // Chèn chuỗi s1 vào giữa s2 |
| char* s = insert(s1, s2); |
| printf("s = %s\n", s); |
| // Giải phóng vùng nhớ của s |
| // vì con trỏ s được khởi tạo bởi malloc() / calloc() |
| free(s); |
| return 0; |
| } |
Từ khóa » Chèn Chuỗi S1 Vào Chuỗi S2
-
Làm Thế Nào để Chèn Chuỗi 2 Vào Chuỗi 1 Tại Vị Trí Vt? - Dạy Nhau Học
-
Chen Xau 2 Vao Xau 1 Sua Code Giup [Archive] - Cộng đồng C Việt
-
Chèn Chuỗi Trong C
-
Làm Thế Nào để Chèn Chuỗi 2 Vào Chuỗi 1 Tại Vị Trí Vt?
-
Top 13 Chèn Chuỗi Trong C
-
Top 15 Chèn Xâu S1 Vào đầu Xâu S2
-
Viết Chương Trình Nhập Vào Hai Chuỗi Ký Tự S1 Và S2 đếm Số Lần Xuất ...
-
Chuỗi Ký Tự (String) Trong C/C++ | Một Trong Những Kiểu Dữ Liệu Cơ ...
-
Bài Tập C - Nối Chuỗi Trong C - VietTuts
-
Lớp String Và Vector Trong Thư Viện STL
-
Tuần 12. 13. KIỂU KÝ TỰ VÀ KIỂU CHUỖI - Bài Tập Nhập Môn Lập Trình
-
[PDF] Bài 1: Mảng Một Chiều