Chèn Chuỗi S1 Vào Giữa S2 - Gists · GitHub
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 Xâu S1 Vào đầu Xâu S2
-
Trong Ngôn Ngữ Lập Trình Pascal, Thủ Tục Chèn Xâu S1 Vào Xâu S2 ...
-
1. Thủ Tục Chèn Xâu S2 Vào Xâu S1 Tại Vị Trí Thứ 2 ...
-
Top 15 Chèn Xâu S1 Vào đầu Xâu S2
-
Tin Học 11 Bài 12: Kiểu Xâu - HOC247
-
Bài 12: Kiểu Xâu - Tìm đáp án, Giải Bài Tập, để Học Tốt
-
1. Thủ Tục Chèn Xâu S2 Vào Xâu S1 Tại Vị Trí Thứ 2 ... - MTrend
-
Trong Ngôn Ngữ Lập Trình Pascal, Thủ Tục Chèn Xâu S1 Vào ...
-
1. Thủ Tục Chèn Xâu S2 Vào Xâu S1 Tại Vị Trí Thứ 2 ... - Giá-xe-má
-
Giải Bài Tập Tin Học 11 - Bài 12: Kiểu Xâu
-
Lập Trình Nhập Từ Bàn Phím 1 Xâu S1 Và S2. Thực Hiện Chèn ... - Hoc24
-
Lập Trình Nhập Từ Bàn Phím Một Xâu S1 Và S2. Thực Hiện ... - Hoc24
-
Trong Ngôn Ngữ Lập Trình Pascal, Thủ Tục Chèn Xâu S1 Vào Xâu S2 Bắt ...
-
[PPT] In Ra Màn Hình Tên Thí Sinh đó D. Thủ Tục Insert (s1, S2, Vt) Cú Pháp
-
Cho Hai Xâu S1, S2. Viết đoạn Chương Trình Chèn Xâu S1 Vào Giữa S2 ...