함수는 s가 가리키는 문자열에서 ch의 마지막 항목을 찾습니다. 문자에 대한 포인터를 반환하거나 문자열에 ch가 없으면 널 포인터를 반환합니다. 문자열 라이브러리 함수를 사용하지 않고 함수를 작성하려고합니다.포인터를 사용하여 문자열에서 char의 마지막 항목 찾기 (C 언어)
이것은 내가 지금까지 가지고있는 것인데, 나에게 맞는 것처럼 보이지만 결과 문자열을 얻을 수없는 것 같습니다.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <math.h>
char *strrchr2(char *s, char ch);
int main()
{
char str1[100];
char char1;
char result_str[100];
printf("\nEnter a string: ");
gets(str1);
fflush(stdin);
printf("Enter the target char in the string: ");
scanf("%c", &char1);
char * result_str = strrchr2(str1, char1);
printf("Resultant string = %s", result_str);
char * strrchr2(char *s, char ch)
{
int count = 0, offset = 0;
while (*(s + count) != '\0')
{
if (*(s + count) == ch)
offset = count;
count++;
}
return *(s + offset);
}
예상 출력 :
Enter a string: abcdefdfdfghh
Enter the target char in the string: f
Resultant string: fghh
'result_str'을 두 번 선언하고 있습니다 !! –
컴파일러는'return * (s + offset);'라인에서 에러를 출력해야합니다. 'char'를 'char *'로 변환 할 수 없습니다. ' –
'fflush (stdin)'은 정의되지 않은 동작입니다. –