thở oxi giai đoạn cuối

 avatar
NguyenAnhQuan
c_cpp
a year ago
1.9 kB
11
Indexable
#include <iostream>
#include <string.h>
using namespace std;
#define MAX 300

void ChenChuoiTaiVitriK(char s[], char s1[], int k);
void Chuanhoa(char s[]);
int myStrlen(char s[], int k);
bool myStrcat(char s[], char s1[], char s2[]);
void myStrcpy(char s[], int vt, char s1[], int k);
int myStrstr(char s[], char s1[]);

int main()
{
	char s[MAX];

	fflush(stdin);
	fgets(s,300,stdin);

	char s1[MAX];
	myStrcpy(s1, 0, s, 0);
	Chuanhoa(s1); 
	puts(s); cout << s1 << 0;
	puts(s1); 
	return 0;
}

void myStrcpy(char s[], int vt, char s1[], int k)
{
	int cnt = 0;

	for (int i = 0; s1[i] != '\0'; i++)
	{
		s[i] = s1[i];
		cnt++;
	}

	// loai bo ki tu \n (do ham fgets tu dong them vao)
	s[cnt - 1] = '\0';
}	

int myStrlen(char s[], int k)
{
	int cnt = 0;

	for (int i = k; s[i] != '\0'; i++)
		cnt++;

	return cnt;
}

void Chuanhoa(char s[])
{
	// xóa 1 ô
	auto dec = [s](int k) 
	{
        for (int i = k; s[i] != '\0'; i++) 
            s[i] = s[i + 1];
    };

    // thêm 1 ô
	auto inc = [s](int k) 
	{
		int id = myStrlen(s, 0);
		s[id] = ' '; s[id + 1] = '\0';

		int ls = myStrlen(s, 0);
        for (int i = ls - 1; i > k; i--) 
            s[i] = s[i - 1];

        s[k] = ' ';
    };

    // xoa khoang trang o dau
    while (s[0] == ' ') dec(0);

    // xoa khoang trang o cuoi
	int len = myStrlen(s, 0);

	for (int i = len - 1; i > 0; i--)
		if (s[i] != ' ') 
		{
			s[i + 1] = '\0';
			break;
		}

	// xoa khoang trang o giua 
	for (int i = 1; s[i] != '\0'; i++)
		while (s[i] == ' ' && s[i + 1] == ' ') dec(i + 1);


	// CHUAN HOA DAU CHAM

	// xoa khoang cach truoc dau cham
	for (int i = 1; s[i] != '\0'; i++)
		while (s[i] == '.' && s[i - 1] == ' ') dec(i - 1);

	// them khoang cach sau dau cham
	for (int i = 0; s[i] != '\0'; i++)
		if (s[i] == '.' && s[i + 1] != ' ') inc(i + 1);

}


Leave a Comment