0% found this document useful (0 votes)
6 views10 pages

Bai Tap Xaukytu

The document contains seven C programming exercises that focus on string manipulation. Each exercise includes code that performs various tasks such as counting words, finding the longest word, extracting characters, replacing substrings, and formatting strings. The exercises demonstrate different aspects of string handling in C, including input/output, string functions, and character arrays.

Uploaded by

leminhhop757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Bai Tap Xaukytu

The document contains seven C programming exercises that focus on string manipulation. Each exercise includes code that performs various tasks such as counting words, finding the longest word, extracting characters, replacing substrings, and formatting strings. The exercises demonstrate different aspects of string handling in C, including input/output, string functions, and character arrays.

Uploaded by

leminhhop757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Bài 1:

#include <stdio.h>
#include <string.h>

int main() {
char s[256];
char temp[256][256];

fgets(s, sizeof(s), stdin);

int i = 0, d = 0, ans = 0;

if(s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = '\0';


while(s[i] != '\0') {
if(s[i] != ' ') {
temp[ans][d++] = s[i];
} else if(d > 0){
temp[ans][d] = '\0';
d = 0;
ans++;
}
i++;
}

if(d > 0) {
temp[ans][d] = '\0';
ans++;
}

printf("So tu trong xau vua nhap la: %d\n", ans);


for(int i = 0; i < ans; i++) {
printf("%s\n", temp[i]);
}

return 0;
}
Bài 2:
#include <stdio.h>
#include <string.h>

int main() {
char s[256];
char temp[256][256];

fgets(s, sizeof(s), stdin);

int i = 0, d = 0, ans = 0;

if(s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = '\0';


while(s[i] != '\0') {
if(s[i] != ' ') {
temp[ans][d++] = s[i];
} else if(d > 0){
temp[ans][d] = '\0';
d = 0;
ans++;
}
i++;
}

if(d > 0) {
temp[ans][d] = '\0';
ans++;
}
int max = strlen(temp[0]);
for(int i = 1; i < ans; i++) {
if(strlen(temp[i]) > max) {
max = strlen(temp[i]);
}
}

printf("%d", max);

return 0;
}
Bài 3:
#include <stdio.h>
#include <string.h>

int main() {
char S[256];
int N, pos;

printf("Nhap xau: ");


fgets(S, sizeof(S), stdin);
printf("Nhap N: ");
scanf("%d", &N);
printf("Nhap pos: ");
scanf("%d", &pos);
if(S[strlen(S) - 1] == '\n') S[strlen(S) - 1] = '\0';

printf("N ki tu dau la: ");


for(int i = 0; i < N; i++) {
printf("%c", S[i]);
}

printf("\n");

printf("N ki tu cuoi la: ");


for(int i = strlen(S) - N; i < strlen(S); i++) {
printf("%c", S[i]);
}

printf("\n");

printf("N ki tu bat dau tu vi tri pos la: ");


for(int i = pos; i < pos + N; i++) {
printf("%c", S[i]);
}
return 0;
}

Bài 4:
#include <stdio.h>
#include <string.h>

char S[256], T[256], K[256];


int lenS, lenT, lenK;

void precompute() {
printf("Nhap xau S: ");
fgets(S, sizeof(S), stdin);
printf("Nhap xau can tim kiem: ");
fgets(T, sizeof(T), stdin);
printf("Nhap xau muon thay vao: ");
fgets(K, sizeof(K), stdin);
S[strlen(S) - 1] = '\0';
T[strlen(T) - 1] = '\0';
K[strlen(K) - 1] = '\0';
lenS = strlen(S); lenT = strlen(T); lenK = strlen(K);
}

void solve() {
char result[512];
int i = 0, j = 0;
while (i < lenS) {
if (strncmp(S + i, T, lenT) == 0) {
strcpy(result + j, K);
j += lenK;
i += lenT;
} else {
result[j++] = S[i++];
}
}

result[j] = '\0';
printf("Chuoi sau khi thay the: %s\n", result);
}

int main() {
precompute();
solve();
return 0;
}

Bài 5:
#include <stdio.h>
#include <string.h>

int main() {
char S[256];
int N, k;
printf("Nhap xau S: ");
fgets(S, sizeof(S), stdin);
printf("Nhap so N: "); scanf("%d", &N);
printf("Nhap so k: "); scanf("%d", &k);
if(S[strlen(S) - 1] == '\n') S[strlen(S) - 1] = '\0';

int len = strlen(S);

for(int i = N; i < N + k; i++) {


S[i] = '\0';
}

for(int i = 0; i < len; i++) {


printf("%c", S[i]);
}

return 0;
}
Bài 6:
#include <stdio.h>
#include <string.h>

char S[41], T[41], K[41];


int lenS, lenT, lenK;
void precompute() {
printf("Nhap xau S1: ");
fgets(S, sizeof(S), stdin);
printf("Nhap xau S2: ");
fgets(T, sizeof(T), stdin);
printf("Nhap xau S3: ");
fgets(K, sizeof(K), stdin);
S[strlen(S) - 1] = '\0';
T[strlen(T) - 1] = '\0';
K[strlen(K) - 1] = '\0';
lenS = strlen(S); lenT = strlen(T); lenK = strlen(K);
}

void solve() {
char result[512];
int i = 0, j = 0;

while (i < lenS) {


if (strncmp(S + i, T, lenT) == 0) {
strcpy(result + j, K);
j += lenK;
i += lenT;
} else {
result[j++] = S[i++];
}
}
result[j] = '\0';
printf("Chuoi sau khi thay the: %s\n", result);
}

int main() {
precompute();
solve();
return 0;
}

Bài 7:
#include <stdio.h>
#include <string.h>

int main() {
char A[256], P[256];
printf("Nhap xau: ");
fgets(A, sizeof(A), stdin);
A[strlen(A) - 1] = '\0';
int s = 0, len = strlen(A), d = 0;
while(A[s] == ' ') {
s++;
}

for(int i = s; i < strlen(A); i++) {


if(A[i] != ' ') {
P[d++] = A[i];
} else {
if((A[i] == ' ') && (A[i - 1] != ' ')) {
P[d++] = A[i];
}
}
}
P[d] = '\0';
strlwr(P);
P[0] -= 32;
printf("%s", P);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy