Solved

C++ Strings

Posted on 2004-10-20
5
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to call a web service and get the results posted in a form in PHP 9 99
Mutiple Folder view (5 Folder view) 3 82
Java array 10 89
Java import explained 4 49
This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn different types of Android Layout and some basics of an Android App.
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 …

752 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