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 Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@baonq-me baonq-me/main.c Created November 15, 2018 07:14 Show Gist options
  • 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.
Code Revisions 1 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. 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;
}
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.

Từ khóa » Chèn Xâu Trong C