Solved

C++ Strings

Posted on 2004-10-20
5
221 Views
Last Modified: 2010-04-17
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
Comment
Question by:raghx_2000
5 Comments
 
LVL 2

Expert Comment

by:avinash_sahay
ID: 12367919
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
 

Expert Comment

by:Codealot
ID: 12369798
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
 

Expert Comment

by:5stringBanjo
ID: 12375323
// 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
 
LVL 3

Accepted Solution

by:
HendrikTYR earned 20 total points
ID: 12381245
#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
 
LVL 3

Author Comment

by:raghx_2000
ID: 12684703
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question