Results 1 to 3 of 3

Thread: problem driving me insane!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    6

    problem driving me insane!!

    hi, im in advanced computer science in school, and i have this horrible problem, i can't seem to stop my brain from boggling over it for days.

    how do i remove duplicates out of a string!!???


    if i input this string, "4751244"

    i need to output 1 2 4 5 7 as the numbers that the string is made of.

    i can sort the string, so the string is 1244457 sorted, now i need someway to check to see if the string has duplicates and remove it!

    please post a source or help me understand! here is what i have so far!

    void Integer :: digits()
    {
    int countplace = 0;
    int placeCount = 0;
    int temp = 0;
    int minIndex = 0;
    int a = 0;
    apstring mynum2 = mynum;

    for(int counter = 0; counter < mynum2.length() - 1; counter++)
    {
    minIndex = counter;

    for(placeCount = counter + 1; placeCount < mynum2.length(); placeCount++)
    {
    if(mynum2[placeCount] < mynum2[minIndex])
    minIndex = placeCount;
    }

    temp = mynum2[minIndex];
    mynum2[minIndex] = mynum2[counter];
    mynum2[counter] = temp;

    }



    apstring mynum3 = " ";
    for(int i = 0; i < mynum2.length() - 1; i++)
    {
    if(mynum2[i] == mynum2[i+1])
    a = 1;
    if (a == 0)
    {

    mynum3 += mynum2[i];
    mynum3 += mynum2[i+1];
    a = 0;
    }

    a = 0;

    }
    cout << mynum3;

    }
    "go masturbate with a cheese grater"

  2. #2
    jim mcnamara
    Guest
    use pointers.
    ansi c version --

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
                    char tmp[20];
                    char n[20];
                    strcpy(n,"0011233445566789");
                    memset(tmp,'\0',sizeof(tmp) ); // this avoids placing zero at end of string
                    dedupe(tmp,n);
                    printf("%s\n",tmp);
                    return 0;
    }
    
    void dedupe(char *ret, char *in){
           char *a,*b,*c;
    	  c=ret;
    	  a=in;
    	  b=a+1;
    	  while (*a!='\0'){
    		  if(*a != *b) *c++=*a;
    		  a++;
    		  b++;
    	  }
    	 
    
    }

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    6
    thanx
    "go masturbate with a cheese grater"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width