Solved

C++ Strings

Posted on 2004-10-20
5
215 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now