No Duplicates in String Array

Create a function that will not insert existing string in the array

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 3427 Views - Last Post: 25 October 2010 - 10:36 AM Rate Topic: -----

#1 laupkram  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 25-August 09

No Duplicates in String Array

Posted 23 October 2010 - 07:26 AM

I am using borland C++ 3.0 in programming C++ and here is my problem

I want to create a function that will not create duplicate values in a string array... I am not using string as datatype but i use *char instead because using string datatype is not available in string.h header of borland c++

This is my function

#include<string.h>

char *str[9999]; //this is my string array
int gctr = 0; //will be my pointer for the string array

void str_table(char *input){
    int ctr=0, flag=0;
    while(ctr!=9999){
       if(strcmp(str[ctr],input)==0){
            flag++; 
       }
       ctr++;
    }
    if(flag==0){
       str[gctr]=input; 
       gctr++; 
    }
}



the result of this function is good upon the first usage of the function... but the next use will be a terrible disaster... the strcmp compares the *input and the *str as an equal value... I tried showing the values of the *input and *str in the while statement showing that *str and *input has equal values so the insertion of the string is denied...

Any suggestions so I can get out of my problem??? thanks :sweatdrop:

This post has been edited by laupkram: 23 October 2010 - 07:41 AM


Is This A Good Question/Topic? 0
  • +

Replies To: No Duplicates in String Array

#2 newclearner  Icon User is offline

  • D.I.C Regular

Reputation: 103
  • View blog
  • Posts: 302
  • Joined: 29-September 10

Re: No Duplicates in String Array

Posted 23 October 2010 - 07:37 AM

How about using some indentation in your code? :mellow:
Was This Post Helpful? 0
  • +
  • -

#3 laupkram  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 25-August 09

Re: No Duplicates in String Array

Posted 23 October 2010 - 07:40 AM

View Postnewclearner, on 23 October 2010 - 06:37 AM, said:

How about using some indentation in your code? :mellow:


iam trying to press tab... sorry... I will be doing some spaces ^_^
Was This Post Helpful? 0
  • +
  • -

#4 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: No Duplicates in String Array

Posted 23 October 2010 - 07:46 AM

View Postlaupkram, on 23 October 2010 - 08:26 AM, said:

I am using borland C++ 3.0

That's your first problem Borland C++ 3.0 is very outdated and isn't good to use.

I recommended Microsoft visual C++ 2010 (or 2008) express edition (it's free) I use it and i love it but there are plenty of other good compilers if you don't like that one.

Link to download: http://www.microsoft...ress/downloads/

EDIT: The compiler your using at the moment is 19 years old.

Also <string.h> is the old way, the new standard is <string>

I hope that was helpful :)

This post has been edited by Alex6788: 23 October 2010 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#5 laupkram  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 25-August 09

Re: No Duplicates in String Array

Posted 23 October 2010 - 07:51 AM

View PostAlex6788, on 23 October 2010 - 06:46 AM, said:

View Postlaupkram, on 23 October 2010 - 08:26 AM, said:

I am using borland C++ 3.0

That's your first problem Borland C++ 3.0 is very outdated and isn't good to use.

I recommended Microsoft visual C++ 2010 (or 2008) express edition (it's free) I use it and i love it but there are plenty of other good compilers if you don't like that one..

Link to download: http://www.microsoft...ress/downloads/

Also <string.h> is the old way, the new standard is <string>

I hope that was helpful :)



I been using Visual Studio and NetBeans for almost 2 years but I am a student that needs to follow what our professor want us to use... by the way Thank You(CAN YOU JOIN ME PROTESING ON MY PROF?? GEEZ...)
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,028
  • Joined: 08-August 08

Re: No Duplicates in String Array

Posted 23 October 2010 - 07:54 AM

char *str[9999]; //this is my string array


http://www.cplusplus...um/beginner/21/
No, it's a pointer to an array of characters.
Try:
char str[9999][200];


which will give you an array of characters with 9999 rows and 200 columns. You can use these assemble 9999 strings of length 200 characters.
Was This Post Helpful? 0
  • +
  • -

#7 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: No Duplicates in String Array

Posted 23 October 2010 - 07:57 AM

View Postlaupkram, on 23 October 2010 - 08:51 AM, said:

View PostAlex6788, on 23 October 2010 - 06:46 AM, said:

View Postlaupkram, on 23 October 2010 - 08:26 AM, said:

I am using borland C++ 3.0

That's your first problem Borland C++ 3.0 is very outdated and isn't good to use.

I recommended Microsoft visual C++ 2010 (or 2008) express edition (it's free) I use it and i love it but there are plenty of other good compilers if you don't like that one..

Link to download: http://www.microsoft...ress/downloads/

Also <string.h> is the old way, the new standard is <string>

I hope that was helpful :)



I been using Visual Studio and NetBeans for almost 2 years but I am a student that needs to follow what our professor want us to use... by the way Thank You(CAN YOU JOIN ME PROTESING ON MY PROF?? GEEZ...)

Oh ok good, I don't know why teachers make students use that when there are plenty of good free compilers.
Was This Post Helpful? 0
  • +
  • -

#8 laupkram  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 25-August 09

Re: No Duplicates in String Array

Posted 23 October 2010 - 07:57 AM

View PostCTphpnwb, on 23 October 2010 - 06:54 AM, said:

char *str[9999]; //this is my string array


http://www.cplusplus...um/beginner/21/
No, it's a pointer to an array of characters.
Try:
char str[9999][200];


which will give you an array of characters with 9999 rows and 200 columns. You can use these assemble 9999 strings of length 200 characters.


Do you mean two dimensional array??? Will this work with strcmp?
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,028
  • Joined: 08-August 08

Re: No Duplicates in String Array

Posted 23 October 2010 - 08:23 AM

Hmm, probably not. This is why I prefer to use strings. I've always thought char was a ridiculous type.
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,028
  • Joined: 08-August 08

Re: No Duplicates in String Array

Posted 23 October 2010 - 08:29 AM

This does work!
#include <iostream>
int main (int argc, char * const argv[]) {
	char st[] = "some text";
	char tst[][200] = {"abc","some text", "defg"};
	if (strcmp(st, tst[1]) == 0) {
		std::cout << "it works.";
	}
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#11 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,090
  • Joined: 30-May 10

Re: No Duplicates in String Array

Posted 23 October 2010 - 08:49 AM

> char str[9999][200];
Bear in mind that if the OP is stuck with the fossil compiler, that they'll be limited to 64Kb for any single object.

> but I am a student that needs to follow what our professor want us to use
Who's paying for the course?
And are you getting your money's worth?

You might not be handing out the $$$ today for your course, but by the same token, you won't be raking in the $$$ because what you've learnt is obsolete.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: No Duplicates in String Array

Posted 23 October 2010 - 09:12 AM

Regardless of compiler, globals are always bad.

I wrote a quick test case:
#include<stdio.h>

void str_table(char *str[], int *size, char *input) {
	int ctr=0, flag=0;
	while (ctr<*size) {
		if (strcmp(str[ctr],input)==0) {
			flag = 1;
			break;
		}
		ctr++;
	}
	if (flag==0) { str[(*size)++] = input; }
}

void show(char *str[], int size) {
	int i;
	for(i=0; i<size; i++) {
		printf("%s\n", str[i]);
	}
}

#define MAX_STRINGS 1000
int main() {
	char *str[MAX_STRINGS];
	int gctr = 0;
	
	str_table(str, &gctr, "Alpha");
	str_table(str, &gctr, "Bravo");
	str_table(str, &gctr, "Bravo");
	str_table(str, &gctr, "Charlie");
	
	show(str, gctr);
	
	return 0;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: No Duplicates in String Array

Posted 23 October 2010 - 07:16 PM

You have made a fundamental mistake at the beginning of this thread which has lead everyone off on a tangent to the real path you want to follow.

This mistake happened here

View Postlaupkram, on 23 October 2010 - 11:26 PM, said:

string datatype is not available in string.h header of borland c++


This has nothing to do with Borland. <string.h> would not give you access to the string datatype in any compiler.
<string.h> is the header for C-strings (character arrays).
<string> is the header for C++ strings.
The '.h' completely changes the library you are including. Sometimes little things have huge consequences.

Change your include to use <string> not <string.h> and suddenly you will have access to the string datatype and everything you have done in this thread in C in reaction to your original fundamental mistake becomes redundant and you can do this challenge in 'real' C++ code using the string library.
http://www.cplusplus...ference/string/

This post has been edited by janotte: 23 October 2010 - 07:18 PM

Was This Post Helpful? 1
  • +
  • -

#14 laupkram  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 25-August 09

Re: No Duplicates in String Array

Posted 25 October 2010 - 06:46 AM

View Postjanotte, on 23 October 2010 - 06:16 PM, said:

You have made a fundamental mistake at the beginning of this thread which has lead everyone off on a tangent to the real path you want to follow.

This mistake happened here

View Postlaupkram, on 23 October 2010 - 11:26 PM, said:

string datatype is not available in string.h header of borland c++


This has nothing to do with Borland. <string.h> would not give you access to the string datatype in any compiler.
<string.h> is the header for C-strings (character arrays).
<string> is the header for C++ strings.
The '.h' completely changes the library you are including. Sometimes little things have huge consequences.

Change your include to use <string> not <string.h> and suddenly you will have access to the string datatype and everything you have done in this thread in C in reaction to your original fundamental mistake becomes redundant and you can do this challenge in 'real' C++ code using the string library.
http://www.cplusplus...ference/string/


let me see if this works... by the way thanks... ^_^

View PostSalem_c, on 23 October 2010 - 07:49 AM, said:

> char str[9999][200];
Bear in mind that if the OP is stuck with the fossil compiler, that they'll be limited to 64Kb for any single object.

> but I am a student that needs to follow what our professor want us to use
Who's paying for the course?
And are you getting your money's worth?

You might not be handing out the $$$ today for your course, but by the same token, you won't be raking in the $$$ because what you've learnt is obsolete.


I just love the shortcuts of Borland C++... i feel like a hardcore programmer even im not hahah... but thanks for your suggestion... Iam more focused in studying C# and java using netbeans and visual studio... I just do it in C++ because of the requirement of our professor
Was This Post Helpful? 0
  • +
  • -

#15 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: No Duplicates in String Array

Posted 25 October 2010 - 06:58 AM

View PostCTphpnwb, on 23 October 2010 - 06:54 AM, said:

No, it's a pointer to an array of characters.


It's actually an array of pointers to characters, you should read from right to left (with the exception of when someone puts const on the left-side of the variable name).

Anyway, the reason string.h doesn't contain a C++ string is because it's a C header file, it is not "the old way", it's an entirely different language's way of doing things.

Try this to see if your compiler and its provided libraries really are as terrible as you claim:
#include <string>
int main()
{
	std::string a = "c++ string :D/>";
	
	return 0;
}



Does this compile for you?

Edit: I didn't see Janotte's post, I'll keep this just to show my agreement.

This post has been edited by oscode: 25 October 2010 - 07:03 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2