Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
i have given two input strings like

HELLO
OPEN

then string1 (HELLO) compares with string2(OPEN) then same characters are deleted from string1.

output is

HLL

please tell me if any one knows.


Thanks in adv.
Posted
Updated 4-Jun-12 10:30am
v2
Comments
Prasad_Kulkarni 4-Jun-12 1:17am    
Homework??
sankar071237 4-Jun-12 2:28am    
ya.

you know the answer????
Prasad_Kulkarni 4-Jun-12 2:35am    
Yes, Have you tried anything so far? If yes, then I will help you to move on..

As I criticised three of the other solutions up there I thought I'd stick my money where my mouth was and write one of me own:
C++
/***/

int is_capital_letter( char to_test )
{
    return to_test <= 'Z' && to_test >= 'A';
}

/***/

unsigned capital_letter_to_index( char letter )
{
    return letter - 'A';
}

/***/

void mark_letter_to_be_eliminated(
    char elimination_table[ 26 ],
    char letter_to_eliminate )
{
    elimination_table[ capital_letter_to_index( letter_to_eliminate ) ] = 1;
}

/***/

int is_letter_to_be_kept( const char elimination_table[ 26 ], char letter_to_test )
{
    return !elimination_table[ capital_letter_to_index( letter_to_test ) ];
}

/***/

void build_elimination_table(
    const char *to_eliminate,
    char elimination_table[26] )
{
    for( ; *to_eliminate; to_eliminate++ )
    {
        if( is_capital_letter( *to_eliminate ) )
        {
            mark_letter_to_be_eliminated( elimination_table, *to_eliminate );
        }
    }
}

/***/

void remove_duplicates_using_elimination_table(
    const char *source,
    const char elimination_table[26],
    char *destination,
    const char *end_of_destination )
{
    while( *source && destination < end_of_destination )
    {
        if( is_capital_letter( *source ) )
        {
            if( is_letter_to_be_kept( elimination_table, *source ) )
            {
                *destination++ = *source;
            }

            source++;
        }
    }

    *destination = 0;
}

/***/

void remove_duplicates(
    const char *source,
    const char *to_eliminate,
    char *destination,
    const char *end_of_destination )
{
    char elimination_table[26] = { 0 };

    build_elimination_table( to_eliminate, elimination_table );

    remove_duplicates_using_elimination_table(
            source,
            elimination_table,
            destination,
            end_of_destination );
}
This lot works by creating a table of letters that are to be eliminated from the source string as it's copied to the destination string. The functions are all pretty safe and won't crash with invalid input they'll just not copy characters that aren't capital letters. And it'll still work if the source and destination are aliased.

Unlike some of the approaches above which have a complexity of O(n*m) where n is the size of the elimination string and m is the size of the string to be copied, this approach has a complexity of O(n+m).

Anyway you'd call it something like this:
C++
char source[] = "HELLO";
char to_eliminate[] = "OPEN";

remove_duplicates( source, to_eliminate, source, source + sizeof source );
which will show it still works when source and destination refer to the same memory.

Final points: it's broken up into so many functions because...
- I have trouble reading functions with more than one loop or one conditional
- It's easier to unit test small functions
 
Share this answer
 
Comments
nv3 4-Jun-12 17:01pm    
When the strings are long this is the absolute winner. The approach via an elimination table is very professional. What I like is the simplicity of the code; what I don't like is that it's a bit lengthy -- but better length and right than short and wrong :-)
Aescleal 5-Jun-12 3:30am    
The reason there are loads of functions is to make unit testing far easier - the functions are defined almost in the order I wrote them. I didn't include the tests as they rely on a C++ unit test framework and defeated the idea of the question being for C.
TRK3 4-Jun-12 19:49pm    
OP said "same characters" not "same letters". The way I read that, he means any characters... Not just letters.

So, we'll have to backup and define "characters" -- if we are dealing with ascii - then your solution gets a lot simpler with a 256 byte elimination table (and no check for capital letters).

If we are dealing with unicode characters then it's a whole different issue.
Aescleal 5-Jun-12 3:25am    
The original poster only supplied one test case and it just had capitals in it. I suppose I could have gone the extreme programming route and just copied "HLL" to the destination but that would have been rather trivial.

Handling UCS 2 characters could be done in the same way, the full character set only occupies 128K which isn't a lot by modern standards, especially on 64 bit machine.
TRK3 5-Jun-12 13:35pm    
Yeah, as long as we aren't including chinese pictographs...

Clearly, we are having more fun with this problem than OP. :^)
Brutal force approach, connoisseurs should forgive (hopefully)

C++


C#
void StringTransform()
{
	const int size = 100;
	char a[size], b[size], output[size];
	
	// zeroise
	memset(a, 0, size);
	memset(b, 0, size);
	memset(output, 0, size);

	// set to initial content
	strcpy(a, "HELLO");
	strcpy(b, "OPEN");

	BOOL found;
	int k = 0;

	for (int i = 0 ; i < size ; i++)
	{
		found = FALSE;
		for (int j = 0 ; j < size ; j++)
		{
			if (a[i] == b[j])
			{
				found = TRUE;
				break;
			}
		}
		// only copy from a to output if not found in b
		if (!found)
		{
			output[k++] = a[i];
		}
	}
}
 
Share this answer
 
v2
Comments
Aescleal 4-Jun-12 5:59am    
Doesn't strike me as being that brute force and quite a direct method. However (which is why I've given it a 3)...

- It's not written in C
- I'd eliminate the variable found, it just complicates things needlessly
- Give the function four parameters and allow aliasing of the destination array with the first input string
- You've made it a variable number arguments function for no reason I can see!
michaelmel 4-Jun-12 20:32pm    
1) Droplets of C++ won't hurt. Makes it more compact. The only non ANSI-C thing in the code is BOOL type and initialization for i, j, k variables. Something for the poster to ponder on (after all, we are doing his homework, aren't we?)
2) It is written intentionally in a way that a student can understand hence "found" variable. It is HOMEWORK, remember? Besides, I cannot see how it can be eliminated without changing the solution substantially.
3)
It is not a "variable number of arguments function" - where did you get that idea?
Try:

C++
#include <stdio.h>
#include <stdlib.h>
int main()
{
  char s[] = "HELLO";
  char t[] = "OPEN";

  size_t LS = sizeof(s) / sizeof(s[0])-1;
  size_t LT = sizeof(t) / sizeof(t[0])-1;

  size_t i, j, k, deleted;

  deleted = 0;

  i = LS;
  while (i--)
  {
    for(j=0; j<LT; j++)
    {
      if (s[i] == t[j])
      {
        deleted++;
        for (k=i; k<LS-deleted-1; k++)
        {
          s[k] = s[k+1];
        }
        break;
      }
    }
  }
  s[LS-deleted] = '\0';

  printf("%s \n", s);

  return 0;
}
 
Share this answer
 
Old School Solution...

C++
void DeleteCommonChars(char * szString1, char * szString2)
{
	char * pchar = szString1;

	while (pchar = strpbrk(pchar, szString2))
		memmove(pchar, pchar + 1, strlen(pchar));
}


Or by blocks as Ascleal's solution demonstrates...

C++
void DeleteCommonCharsByBlocks(char * szString1, char * szString2)
{
	char * pFindChar = szString1;
	char * pDestChar = szString1;
	char * pNextStart = szString1;

	while (pFindChar = strpbrk(pNextStart, szString2))
	{
		size_t MoveSize = (size_t) (pFindChar - pNextStart);

		if (MoveSize)
		{
			memmove(pDestChar, pNextStart, MoveSize);

			pDestChar  += MoveSize;
		}

		pNextStart = pFindChar + 1;
	}

	*pDestChar = '\0';
	strcpy(pDestChar, pNextStart);
}
 
Share this answer
 
v5
Comments
JackDingler 4-Jun-12 17:19pm    
An optimization might use a reverse find...

Another might optimize the strlen() out of the loop...
Here is the solution

Program :-

VB
#include<stdio.h>
#include<string.h>

main()
{
   char a[100], b[100];

   printf("Enter the first string\n");
   gets(a);

   printf("Enter the second string\n");
   gets(b);

   if( strcmp(a,b) == 0 )
      printf("Entered strings are equal.\n");
   else
      printf("Entered strings are not equal.\n");

   return 0;
}


For More :-
http://www.programmingsimplified.com/c-program-compare-two-strings[^]


Thanks
 
Share this answer
 
Comments
sankar071237 4-Jun-12 2:20am    
your just comparing.

i have given
HELLO is the first string and
OPEN is the second string then

HLL is my out put.

it means both string contains "E","O" same characters then delete those characters in first string and display.
[no name] 4-Jun-12 2:36am    
Hey ! i am not going to do your homework Ok .
[no name] 4-Jun-12 2:37am    
Just compare & get the function by yourself for deleting it .
Anurag Sarkar 4-Jun-12 2:41am    
You are going yo use one more IF
CPallini 4-Jun-12 3:46am    
What has it to do with OP question?
C#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int i = 0,j=0,c = 0;
    int hasFind = 0;
    char str1[100],str2[100],str3[100];
    puts("please input string1(len<100):");
    gets(str1);
    puts("please input string2(len<100):");
    gets(str2);
    for(i=0;i<strlen(str1);++i)
    {
        hasFind = 0;
        for(j=0;j<strlen(str2);++j)
        {
            if(str1[i]==str2[j])
            {
                hasFind = 1;
                break;
            }
        }
        if(!hasFind)
        {
            str3[c++] = str1[i];
        }
    }
    str3[c] = '\0';
    printf("string1 deleted the same characters with string2 is:\n%s\n",str3);
    system("pause");
    return 0;
}
 
Share this answer
 
Comments
Aescleal 4-Jun-12 6:05am    
I couldn't with any conscience give you anything more than a 1 for this - you used gets which means any idiot can crash the function by typing in loads of characters - i.e. just leaning on the keyboard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900