Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

C++ Strings

Hello there

I have 2 strings:
S1 = "ABCDG" and S2 = "ABCDEF"
Now i want to eliminate the common letters and display the rest i.e., GEF.
How can i write a program to do this in C++?

i need a code so that i can understand...

regards...
in advance
0
raghx_2000
Asked:
raghx_2000
1 Solution
 
avinash_sahayCommented:
I hope this is not for any homework.
What data types are you using for strings? Let us assume it is char array.

In a loop, take each character of S1. Search for this character in S2 using strchr. If not found, add the character to a third array. If found, then replace that character in S2 by something that you know will not be present in any of the two strings. Example: character '0' (zero) if you are sure that this character will not be present in S1 and S2.

After the loop, get all the characters of S2 and add them to the third array.

0
 
CodealotCommented:
I think that strategy won't work when one character is present multiple times; like "ABCDG" "ABCDEFA". So, I think the key is actually to find out what characters the two strings share. The code for that could be something like:

char*      S1("ABCDG");
char*      S2("ABCDEF");
long      size=sizeof(char*);
char*      shared = (char*) malloc(size);                                                
unsigned int c = 0;
      
for(unsigned int i=0; i<strlen(S2); i++) {            
      if (strchr(S1, S2[i]))      {
            shared = (char*) realloc( shared, size += sizeof(char));
            shared[c]=S2[i];
            c++;
      }            
}
shared[c] = 0;
printf( shared );
free(shared);

Next, you loop over S1 and S2 and put all characters that aren't in the shared array in a result array.
0
 
5stringBanjoCommented:
// Use the Standard Template Library
// Always handy.....
#include <string>
#include <iostream>
#include "strhelpers.h"

using namespace std;

string EliminateCommonLetters(string a, string b)
{
  string newa = "", newb = "";

  // first we collect all chars that are in b and not in a
  for (int i = 1; i < a.size(); i++)
    if (b.find(a[i]) == b.npos)
      newa = newa + a[i];

  // Then we copy b to the collected chars and check these against
  // string a.
  newb = newa + b;
  for (int i = 0; i < newb.size(); i++)
    if (a.find(newb[i]) == a.npos)
      newa = newa + newb[i];

  // return the new string
  return newa;
}


int main(int argc, char **argv)
{
  cout << EliminateCommonLetters("ABCDG" ,"ABCDEF") << endl ;

  return 0;
}
0
 
HendrikTYRCommented:
#include <iostream>
using namespace std;

void remove_duplicates(char *s1, char *s2, char *result) {
    char ascii[256];
    memset(ascii, 0, 256);

    while(*s1) ascii[*s1++] = 1;
    while(*s2) {
        if(!ascii[*s2]) ascii[*s2] = 3;
        if(ascii[*s2] == 1) ascii[*s2] = 2;
        s2++;
    }
   
    int i = 0;
    while(i < 256) if(ascii[i++] % 2) *result++ = i-1;
    *result = 0;
}

int main() {
    char s1[] = "ASDMNABFSDFKJDSKHASDI";
    char s2[] = "IOUEWRWERBILSDFLJLXCD";

    char r[256];

    remove_duplicates(s1, s2, r);

    cout << r << endl;

    return 0;
}
0
 
raghx_2000Author Commented:
Hi there !

How can i arrange a sentence in an alphabetical order in C++.
Example: Suppose i have a sentence like
Question:- Happy Xmas Or Christmas
Answer :- Christmas Happy Or Xmas

Please help me with the code.

regards
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now