Thread: Transferring non-repeated characters to another string

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    Transferring non-repeated characters to another string

    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?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's not easier to remove a character in place, it's possible to shift all characters back one step to achieve that, but it's going to be simpler IMO to copy to a new array.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is no pre-built function in C to remove repeating char's, in place.

    There is a simpler, more efficient way to do this job, however. All letter values in the computer character set, are contiguous, and represented as small 8 bit int's. So it's easy to treat them as indices to an array of int's.

    I see your eyes starting to glaze over there, but stick with me!

    Let's take an example where the input is all lowercase, to make it easier. If you printf("%c",97), in a program, what do you see? What about printf("%d",97); what do you see now?

    Char's are simply small range int's, with only 8 bits.

    So if we had an array and were trying to count all the occurrences , we could count them like so:
    Code:
    for(each i in the int array of count[]) {
        count[i] = 0;
    }
    //now
    for(each letter in the string[]) {
       count[string[i]]++;
    }
    Now print count, and each count[i] that == 1, you want to print. Don't print the rest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  2. To check if a sequence is repeated in a string
    By chatterjee in forum C Programming
    Replies: 6
    Last Post: 08-18-2006, 09:40 PM
  3. Can repeated characters be printed with a C statement?
    By Pete_2000 in forum C Programming
    Replies: 4
    Last Post: 05-05-2006, 07:47 PM
  4. transferring GDI attributes.
    By Sebastiani in forum Windows Programming
    Replies: 4
    Last Post: 11-19-2002, 02:30 PM
  5. Concatenating: characters->string->vector (string) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2002, 01:14 PM

Tags for this Thread