Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

새싹교실/2020/새싹부터나무까지/박소은/실습/20.10.09: Difference between revisions

From ZeroWiki
No edit summary
(Repair batch-0006 pages from live compare)
 
(8 intermediate revisions by one other user not shown)
Line 1: Line 1:
== 1 ==
== 1 ==
#include <stdio.h>
  //strcpy
  //*를 사용하되, 포인터(p)를 사용하지 마시오.
   
   
  int main(void)
  char* strcpy(char s1[], char s2[])
  {
  {
  int number[10] = { 1,3,5,7,9,11,13,15,17,19 };
  int i = 0;
  int i, * p;
  while (*(s2 + i))
for (i = 0; i < 10; i++)
  {
  {
  printf("%d\n", *(number + i));
  s1[i] = s2[i];
i++;
  }
  }
s1[i] = s2[i];
  return 0;
  return s1;
  }
  }
== 2 ==
== 2 ==
  //strlen
  //strlen
   
   
  int strlen(const char* p)
  int strlen(char* p)
  {
  {
  int length = 0, i = 0;
  int length = 0, i = 0;
Line 30: Line 28:
  //strcat
  //strcat
   
   
  void strcat(char s1[], const char s2[])
  void strcat(char s1[], char s2[])
  {
  {
  for (int i = strlen(s1); i < strlen(s1) + strlen(s2); i++)
  for (int i = strlen(s1), j = 0; i <= strlen(s1) + strlen(s2); i++, j++)
  {
  {
  s1[i] = s2[i];
  s1[i] = s2[j];
  }
  }
  }
  }
== 4 ==
== 4 ==
#include <stdio.h>
  //strcmp
  //포인터를 사용하지 마시오.
   
   
  int main(void)
  int strcmp(char s1[], char s2[])
  {
  {
  int number[3][4] = { {1,3,5,7},{9,11,13,15},{17,19,21,23} };
  int i = 0;
  int i, j, * p;
  while (s1[i] != '\0' || s2[i] != '\0')
for (i = 0; i < 3; i++)
  {
  {
  for (j = 0; j < 4; j++)
  if (s1[i] > s2[i])
  {
return 1;
printf("%d\n", *(*(number + i) + j));
  else if (s1[i] < s2[i])
  }
return -1;
  i++;
  }
  }
  return 0;
  return 0;
  }
  }

Latest revision as of 01:08, 27 March 2026

1

//strcpy

char* strcpy(char s1[], char s2[])
{
	int i = 0;
	while (*(s2 + i))
	{
		s1[i] = s2[i];
		i++;
	}
	s1[i] = s2[i];
	return s1;
}

2

//strlen

int strlen(char* p)
{
	int length = 0, i = 0;
	while (*(p + i)) {
		length++;
		i++;
	}
	return length;
}

3

//strcat

void strcat(char s1[], char s2[])
{
	for (int i = strlen(s1), j = 0; i <= strlen(s1) + strlen(s2); i++, j++)
	{
		s1[i] = s2[j];
	}
}

4

//strcmp

int strcmp(char s1[], char s2[])
{
	int i = 0;
	while (s1[i] != '\0' || s2[i] != '\0')
	{
		if (s1[i] > s2[i])
			return 1;
		else if (s1[i] < s2[i])
			return -1;
		i++;
	}
	return 0;
}