-
Notifications
You must be signed in to change notification settings - Fork 0
/
charRemFromString.c
70 lines (63 loc) · 1.44 KB
/
charRemFromString.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#define Size 20
//write a method which will remove any given character from a string ?
int getLength(char str[Size]) {
int i, length = 0;
for (int i = 0; str[i] != '\0'; i++)
length++;
return length;
}
void removeCharFromString(char ch,char str[Size]){
int len,i;
len=getLength(str);
for(i=0;i<len;i++){
if(str[i] == ch){
for(int j=i;j<len;j++){
str[j]=str[j+1];
}
len--;
i--;
}
}
str[i]='\0';
printf("%s",str);
}
void removeCharFromStringUsingTemp(char ch,char *str){
char temp[Size];
int len=strlen(str),j=0;
for(int i=0;i<len;i++){
if(str[i]!=ch){
temp[j++]=str[i];
}
}
temp[j]='\0';
printf("%s",temp);
}
void removeDuplicateCharFromString(char ch,char *str){//remove duplicate means only unique
int len=strlen(str);
for(int i=0;i<len;i++){
for(int j=i+1;j<len;j++){
if(str[i]==str[j]){
for(int k=j;k<len;k++){
str[k]=str[k+1];
}
len--;
j--;
}
}
}
str[strlen(str)]='\0';
printf("%s",str);
}
int main(){
char str[Size],ch;
printf("write a sentence :\n");
gets(str);
printf("Enter a character to remove :\n");
scanf(" %c",&ch);
removeCharFromStringUsingTemp(ch,str);
return 0;
}
/*
*/