Inflearn Community Q&A
[연습문제풀이2]
Written on
·
178
1
while과 break만 사용하고 마지막에 주신 힌트가 ++ptr인데
++ptr 이렇게 전위증가를 사용하면 맨 앞의 J가 출력이 안되더라고요 그래서 -1을 사용해봤습니다.
조건을 완벽하게 갖추진 못했네요 . break가 빠졌으니까요.
일단 진도 나간후에 다시 생각해봐야겠네요.
<code>
// 세 번째 시도
while (*ptr != '\0')
{
cout << *(++ptr - 1);
}
cout << "##" << endl;
<output>
Jack jack##
<전체코드>
#include <iostream>
using namespace std;
int main()
{
char name[] = "Jack jack";
const int n_name = sizeof(name) / sizeof(name[0]);
char *ptr = name;
// 첫 번째 시도
int i = 0;
while (i < n_name)
{
if (*(ptr + i) == '\0')
break;
cout << *(ptr + i);
++i;
}
cout << "##" << endl; //널 캐릭터 확인용
// cout << *(ptr++) << endl;
// cout << *(ptr++) << endl;
// cout << *(ptr++) << endl;
// cout << *(ptr++) << endl;
// 두 번째 시도, 굳이 조건이 n_name이 아니어도 됐을 텐데...
// while (n_name)
// {
// if (*ptr == '\0')
// break;
// cout << *(ptr++);
// }
// cout << "##" << endl; //널 캐릭터 확인용
// cout << *ptr-- << endl; // 감소 사용
// cout << *ptr-- << endl;
// cout << *ptr-- << endl;
// 세 번째 시도
while (*ptr != '\0')
{
cout << *(++ptr - 1);
}
cout << "##" << endl;
// 네 번째 시도
// for (int i = 0; i < n_name; ++i)
// {
// // cout << char(toupper(name[i])) << endl;
// cout << *(ptr + i);
// }
// cout << "##" << endl;
// cout << endl;
// long long array[] = {9, 7, 5, 3, 1};
// long long *ptr = array;
// for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
// {
// cout << *(ptr + i) << " " << (uintptr_t)(ptr + i) << endl;
// }
// short value = 7;
// short *ptr = &value;
// cout << uintptr_t(ptr - 1) << endl;
// cout << uintptr_t(ptr) << endl;
// cout << uintptr_t(ptr + 1) << endl;
return 0;
}
C++
Answer
This question is waiting for answers
Be the first to answer!





