ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

 

์ž…๋ ฅ์˜ˆ์‹œ 1

5 KimSooJin 010-1234-5678

ParkGilDong 010-2468-2468

ChungSangChul 010-1230-4567

LeeYoungHee 010-1357-2468

ChoiMyungHee 010-3458-1267 2468

 

์ถœ๋ ฅ์˜ˆ์‹œ1

2 ParkGilDong 010-2468-2468

4 LeeYoungHee 010-1357-2468

 

 

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

struct customer {
	char id[5];
	char* name;
	char* phone;
};

int main() {
	struct customer *ct;
	int n, cnt = 0;
	char tmp1[51], tmp2[51];
	int len1 = 0, len2 = 0;
	char num[5];

	scanf("%d", &n);
	ct = (struct customer*)malloc(n * sizeof(struct customer));
	if (ct == NULL) {
		printf("Not enough memory!");
		return -1;
	}

	for (int i = 0; i < n; i++) {
		scanf("%s %s", tmp1, tmp2);
		
		len1 = strlen(tmp1);
		len2 = strlen(tmp2);

		(ct+i)->name = (struct customer*)malloc((len1 + 1) * sizeof(struct customer));
		if ((ct + i)->name == NULL) {
			printf("Not enough memory!");
			return -1;
		}
		strcpy((ct + i)->name, tmp1);

		(ct+i)->phone = (struct customer*)malloc((len2 + 1) * sizeof(struct customer));
		if ((ct + i)->phone == NULL) {
			printf("Not enough memory!");
			return -1;
		}
		strcpy((ct + i)->phone, tmp2);
	}

	scanf("%s", num);

	for (int i = 0; i < n; i++) {
		int k = 0;
		int chk = 0;
		int flag = 0;
		for (int j = 9; j <= 12; j++) {
			if ((ct + i)->phone[j] == num[k]) {
				flag++;
				k++;
				chk = i;
			}
		}
		if (flag == 4) {
			printf("%d %s %s\n", chk + 1, (ct + chk)->name, (ct + chk)->phone);
			cnt++;
		}
	}

	if (cnt == 0)
		printf("none");

	return 0;
}

 

 

*๊ณ ๊ฐ๋ฒˆํ˜ธ ๊ฐ™์€ ๊ฑฐ ๊ฒ€์‚ฌํ• ๋•Œ ๋’ท์ž๋ฆฌ ๋„ค๊ฐœ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜์ง€ ์• ๋จน์—ˆ์Œ. ๋ฐฐ์—ดํ‘œ๊ธฐ ์จ์„œ ํ•œ๊ฒŒ ์œ„์— ์ฝ”๋“œ๊ณ  ๋ฐฐ์—ดํ‘œ๊ธฐ ์•ˆ์“ฐ๊ณ  ๋‹ค์‹œ ํ•ด๋ณผ๊ฑฐ์ž„

 

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

struct customer {
	char id[5];
	char* name;
	char* phone;
};

int main() {
	struct customer *ct;
	int n, cnt = 0;
	char tmp1[51], tmp2[51];
	int len1 = 0, len2 = 0;
	char num[5];

	scanf("%d", &n);
	ct = (struct customer*)malloc(n * sizeof(struct customer));
	if (ct == NULL) {
		printf("Not enough memory!");
		return -1;
	}

	for (int i = 0; i < n; i++) {
		scanf("%s %s", tmp1, tmp2);
		
		len1 = strlen(tmp1);
		len2 = strlen(tmp2);

		(ct+i)->name = (struct customer*)malloc((len1 + 1) * sizeof(struct customer));
		if ((ct + i)->name == NULL) {
			printf("Not enough memory!");
			return -1;
		}
		strcpy((ct + i)->name, tmp1);

		(ct+i)->phone = (struct customer*)malloc((len2 + 1) * sizeof(struct customer));
		if ((ct + i)->phone == NULL) {
			printf("Not enough memory!");
			return -1;
		}
		strcpy((ct + i)->phone, tmp2);
	}

	scanf("%s", num);

	for (int i = 0; i < n; i++) {
		int k = 0;
		int chk = 0;
		int flag = 0;
		for (int j = 9; j <= 12; j++) {
			if (strncmp((ct + i)->phone+9, num, 4)==0) {
				flag++;
				chk = i;
			}
		}
		if (flag == 4) {
			printf("%d %s %s\n", chk + 1, (ct + chk)->name, (ct + chk)->phone);
			cnt++;
		}
	}

	if (cnt == 0)
		printf("none");

	return 0;
}

 

**๋ฐฐ์—ด ํ‘œ๊ธฐ ์•ˆ์“ด๊ฒŒ ์ด๊ฑฐ

strncmp ์ด์šฉํ•ด์„œ strncmp((ct + i)->phone+9, num, 4)==0 ์ด๋ ‡๊ฒŒ ํ•ด์ฃผ๋ฉด 9๋ฒˆ์งธ ์ž๋ฆฌ๋ถ€ํ„ฐ ๊ฒ€์‚ฌ ๊ฐ€๋Šฅํ•œ.

๋ฐ˜์‘ํ˜•

'Programming > c' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[2**][๊ตฌ์กฐ์ฒด] S-๋งˆํŠธ์˜ ๊ณ ๊ฐ๊ด€๋ฆฌ๋ถ€ : ๊ณ ๊ฐ์ •๋ณด ์ž…๋ ฅ๋ฐ›์•„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ €์žฅ, ๋ฒˆํ˜ธ ๊ฐ™๋‹ค๋ฉด ์ด๋ฆ„์ˆœ์œผ๋กœ ์ €์žฅ ํ›„ ์ถœ๋ ฅ  (0) 2021.05.18
[2**] [๊ตฌ์กฐ์ฒด] ํ˜ธํ…” ์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ณ  ์กฐ๊ฑด์— ๋งž๋Š” ์‚ฌ์ „์ƒ ๊ฐ€์žฅ ๋น ๋ฅธ ํ˜ธํ…” ์ถœ๋ ฅํ•˜๊ธฐ  (0) 2021.05.16
[2**] N๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด A๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ๋™์ผํ•œ ๋‹จ์–ด์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ, ๋‹จ์–ด์™€ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅ(*์ค‘๋ณต๋ฌธ์ž๋Š” ํ•œ๋ฒˆ๋งŒ)  (0) 2021.05.16
[2**] N๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฌธ์ž์—ด A๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ ๋ฌธ์ž์—ด์—์„œ ์ค‘๋ณต๋œ ๋‹จ์–ด 1๊ฐœ๋ฅผ ์ฐพ์•„ ํ™”๋ฉด์— ์ถœ๋ ฅ  (0) 2021.05.16
[1-2ํ•™๊ธฐ 2**] ๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›๊ณ  ๊ตฌ๊ฐ„ ์ •ํ•ด์„œ ๊ตฌ๊ฐ„ ์•ˆ์— ์˜์–ด ๋Œ€๋ฌธ์ž๊ฐ€ ๋ช‡๊ฐœ๋‚˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ ํ›„, ๊ฐ€์žฅ ์ ์€ ๊ฒƒ ์ถœ๋ ฅํ•˜๊ธฐ, ๊ฒน์น˜๋ฉด ๋‚˜์ค‘์— ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด ์ถœ๋ ฅํ•˜๊ธฐ  (0) 2021.01.08