Arne Němec
Arne Němec

Reputation: 1

Adding common characters in two strings into a new string

I wrote a function in C that looks like this:

char *common(char *a, char *b){
    char *result = "";
       for (int i = 0; i < strlen(a); i++){
        for (int j = 0; j < strlen(b); j++){
            if (a[i] == b[j]){
                printf("%c", a[i]);
            }
            }
        }
    return result;
    }

It should compare the two given strings and give a new string with the common characters of those strings back. For example, strings: hello world and thank you would give back a string ho.

For now it just prints the characters and the result is empty, because I have a problem with getting the characters in the result string. How can I do that in this case?

Plus the result string shouldn't include duplicate characters, but each letter only once. I have no idea how to do that as well, but that's not the main problem.

Upvotes: 0

Views: 584

Answers (3)

SayAz
SayAz

Reputation: 760

I hope it will help you, Its a working code. I just made very small change in your program and added what required. Soon I will optimized this. But first go through below code to understand the concept.

#include<stdio.h>


int isCharExist(char *str, char c){
 int i=0;
 while(str[i] != NULL){
  if(str[i] == c){
    return 1;
  }
  i++;
 }
 return 0;
}
char *common(char *a, char *b){
    char *result = "";
    int index = 0,i,j;
       for (i = 0; i < strlen(a); i++){
    for ( j = 0; j < strlen(b); j++){
        if (a[i] == b[j]){
        if(!isCharExist(result, a[i])){
         result[index++] = a[i];
         result[index] = '\0';//last character of string is always NULL

        }
        }
        }
    }

    return result;
}

void main(){
  char *result = "",*str1,*str2;
  //better to assign space before assignment
  str1 = "hello world";
  str2 = "thank you";
  //clrscr();
  result = common(str1,str2);
  printf("%s",result);

}

The below code is better than above one and with small changes, I am not removing this one for your better understanding.

#include<stdio.h>
int isCharExist(char *result, char c);
char *common(char *a, char *b);

int isCharExist(char *result, char c){
//this function help you to find that current character
//is in your result or not
 int i=0;
 while(result[i] != NULL){
  if(result[i] == c){
    //if it find character in result string it return 1
    return 1;
  }
  i++;
 }
 return 0;//if not find then it return 0
}

char *common(char *a, char *b){
    char *result = "";
    int index = 0,i,j;

    //it is good to keep length in seprate variable before loop
    //in loop it calculate length each time
    int len1 =  strlen(a);
    int len2 =  strlen(b);
       for (i = 0; i < len1; i++){
    for ( j = 0; j < len2; j++){

    //check equality but not for blank
        if (a[i] == b[j] && a[i] != ' '){
        if(!isCharExist(result, a[i]))
        {
         result[index++] = a[i];
         result[index] = '\0';//last character of string is always NULL

        }
        }
        }
    }

    return result;
}

void main(){
  char *result = "",*str1,*str2;
  //better to assign space before assignment
  str1 = "hello world";
  str2 = "thank you";
  //clrscr();
  result = common(str1,str2);
  printf("%s",result);

}

The above one is just some changes in your function, so just replace your function not complete code.

char *common(char *a, char *b){
    char *result = "";
    int index = 0,i,j,k, isDuplicate=0;

    //it is good to keep length in seprate variable before loop
    //in loop it calculate length each time
    int len1 =  strlen(a);
    int len2 =  strlen(b);
       for (i = 0; i < len1; i++){
    for ( j = 0; j < len2; j++){

    //check equality but not for blank
        if (a[i] == b[j] && a[i] != ' '){
        k = 0;
        while(result[k] != '\0'){
            if(a[i] == result[k]){
              isDuplicate = 1;
              break;
            }
            k++;
        }
        if(!isDuplicate)
        {
         result[index++] = a[i];
         result[index] = '\0';//last character of string is always NULL

        }
        isDuplicate = 0;
        }
        }
    }

    return result;
}

Upvotes: 1

First you need to have an array of char to the store the common characters. Then you need to store them subsequent to the order in which they occur but also proof if the character already were assigned to the array. After the loop was completely going though you have to assign the \0 null character at the following element to indicate the end of the string. Furthermore you have to pass to extra parameters a pointer to a char array in the caller where the string with the common characters is stored and another parameter with the size of that array as its size can´t be determined inside the function:

char *common(char *a, char *b, char *c, size_t len){

    int k = 0;

    for (int i = 0; i < strlen(a); i++){
        for (int j = 0; j < strlen(b); j++){
            if (a[i] == b[j]){
                printf("%c", a[i]);

                for(size_t i = 0; i < size; i++) {  
                   if(c[i] == a[i])
                   {
                      continue;
                   }
                }

                if(i < len)
                {
                   c[k] = a[i];
                }
                else if( i > len )
                {
                   return NULL;
                }

                k++
            }
        }
    }

    x[k] = '\0';    

    return c;
}

Upvotes: 0

MrFinishLine
MrFinishLine

Reputation: 34

Can't give you whole code but can help you with another approach. Simple way would be to make new string variable (result for example) where you would save common characters as you did (you can make it dynamically or with size of longest string between 2 you have), than you go through every element in one string with loop, than check with strchr() function from string.h if character is in another string, if it's in another string check again is it in result, if it's not just concatenate it to result with strcat(). I tried to explain it as clearly as I could. Hope it helps :)

Upvotes: 0

Related Questions