Solved

C++ Strings

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Report events not being triggered 8 120
mapShare challenge 13 109
Device same like our heart 12 86
Image decoding from Camera 3 88
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

803 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