문제
주어진 문자열에서 N – Characters from Position 삭제에 사용자 함수를 작성합니다. 여기에서 문자열은 런타임에 사용자가 제공합니다.
해결책
주어진 문자열에서 n개의 문자를 삭제하는 솔루션은 다음과 같습니다 -
알고리즘
주어진 문자열에서 n자를 삭제하는 알고리즘을 참조하십시오.
1단계 - 시작
2단계 − 런타임 시 문자열 읽기
3단계 − 문자를 삭제해야 하는 위치에서 읽기
4단계 − 읽기 n, 해당 위치에서 삭제할 문자 수
5단계 − deletestr(str,p,n) 함수를 호출하여 7단계로 점프
6단계 − 중지
7단계 - 호출된 함수 deletestr(str,p,n)
1. for i =0 , j = 0 to Length[str] 2. do if i = p-1 3. i = i + n 4. str[j] =str[i] 5. str[j] = NULL 6. print str
예시
다음은 주어진 문자열에서 n자를 삭제하는 C 프로그램입니다. -
#include <stdio.h>
#include <conio.h>
// prototype of function
void del_str(char [],int, int);
main(){
int n,p;
char str[30];
printf("\n Enter the String:");
gets(str);
fflush(stdin);
printf("\n Enter the position from where the characters are to be deleted:");
scanf("%d",&p);
printf("\n Enter Number of characters to be deleted:");
scanf("%d",&n);
del_str(str,p,n);
}
//function call
void del_str(char str[],int p, int n){
int i,j;
for(i=0,j=0;str[i]!='\0';i++,j++){
if(i==(p-1)){
i=i+n;
}
str[j]=str[i];
}
str[j]='\0';
puts(" The string after deletion of characters:");
puts(str);
} 출력
위의 프로그램을 실행하면 다음과 같은 결과가 나온다 -
Enter the String:Tutorials Point C programming Enter the position from where the characters are to be deleted:10 Enter Number of characters to be deleted:6 The string after deletion of characters: Tutorials C programming