Code:
#include<stdio.h>#include<string.h>
#define size 50
void count(char s[size]);
main(){
char str[size];
int num = 7;
printf("Enter a string: ");
scanf("%s", str);
count(str);
}
void count(char s[size]){
char str[size];
int str_cnt[size] = {}, i, j, str_s = strlen(s);
for(i = 0; i < str_s; i++){
if(strlen(str) == 0){
str[0] = s[i];
str_cnt[0]++;
str[1] = '\0';
}
else{
//check if character already exists
for(j = 0; j < strlen(str); j++){
//if it exists then increment
//printf("%c, %c\n", s[i], str[j]);
if(s[i] == str[j]){
str_cnt[j]++;
break;
}
}
//if not, then add to the character array then increment
if(j == strlen(str)){
str_cnt[j]++;
str[j] = s[i];
str[j+1] = '\0';
}
}
}
for(i = 0; i < str[i] != '\0'; i++){
printf("%c - %d\n", str[i], str_cnt[i]);
}
}
This program will count how many times a character was used in a word. Now, I want to modify it so that it will remove letters that was used more than 1. Since I do not know how to remove a character from a string, my idea is to transfer the letters that were only used once to another string.
Example:
Enter string: google
g-2
o-2
l-1
e-1
Output: le
Is this possible? Or is removing(if such function exists) a character much easier?