String Comparsion

This is a discussion on String Comparsion within the C++ Programming forums, part of the General Programming Boards category; Code: int main() { char strngs[100][20]={"BILL","BILL","BILL","JACK","JACK","KEN"}; checkDouble(strngs,6); } void checkDouble(char array[100][20], int len) { int i=0; static int cnt = ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    String Comparsion

    Code:
    int main()
    {
    
    	char strngs[100][20]={"BILL","BILL","BILL","JACK","JACK","KEN"};
    	checkDouble(strngs,6);
    	
    }
    
    void checkDouble(char array[100][20], int len)
    {
    
    	int i=0;
    	static int cnt = 0;
    	if(strcmp(array[i],array[++i]))
    	{
    		++cnt;
    		cout<<"There are "<<cnt << " "<<array[i];
    		checkDouble(array,--len)
    	}
    	else
    		cnt = 0;
    		
    }
    i have created a test string called strngs.
    strngs have 6 index.

    i want to pass it to checkDouble so it will print something like this

    There are 3 BILL
    There are 2 JACK
    There are 1 KEN

    i know my recursion checkDouble is not correct and i dont know how to code this function.
    can i get some help.

    thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Do you need to use recursion?

    You can't print a message every time you increment cnt, or you'll get something like
    Code:
    There are 1 BILL
    There are 2 BILL
    There are 3 BILL
    . . . assuming the rest of the code works properly. Which it won't.

    Code:
    if(strcmp(array[i],array[++i]))
    I'm pretty sure that's undefined behaviour, and you probably want ==0 anyway.

    Code:
    char strngs[100][20]={"BILL","BILL","BILL","JACK","JACK","KEN"};
    This works as well, and you save a lot of space.
    Code:
    char *strngs[100]={"BILL","BILL","BILL","JACK","JACK","KEN"};
    You just can't modify the strings, that's all.

    I think you need to re-think the logic behind your code. How would you determine duplicates? Maybe start with the first name, search for the strings that match it, and then continue on with the second name, and so on. You'd have to set a string to "" or something once you counted it, or you'd count every string more than you wanted to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Code:
    #include <iostream>
    #include <cstring>
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    using namespace std;
    
    void checkDouble(char array[100][20], int len);
    
    
    int main()
    {
    
    	char strngs[100][20]={"BILL","BILL","BILL","JACK","JACK","KEN","KEN","MILO"};
    	checkDouble(strngs,8);
    	
    }
    
    void checkDouble(char array[100][20], int len)
    {
    
    	cout<<"Passed array ";
    	for (int n = 0; n <len; n++)
    	{
    		cout<<array[n]<<" ";
    	}
    	cout <<endl;
    	int cnt = 0;
    	for (int i =0; i <len; i++)
    	{
    		for (int j = ++i; j<len; j++)
    		{
    			if (strcmp(array[i],array[j]))
    				++cnt;
    		}
    		cout<<array[i]<<" "<<cnt<<endl;
    		cnt =1;
    	}			
    		
    }
    somehow this work when i use only 6 strings
    for example
    {"BILL","BILL","BILL","JACK","JACK","KEN"};
    i would get
    BILL 3
    JACK 2
    KEN 1

    but when i pass
    {"BILL","BILL","BILL","JACK","JACK","KEN","KEN","M ILO};
    i would get
    BILL 5
    JACK 4
    KEN 2
    MILO 1

    that is not right.

    what else i need to do.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you'd want to do this:
    Code:
    		for (int j = i+1; j<len; j++)
    That way, you don't increment "i" outside of your loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    when i do that i get

    BILL 5
    BILL 6
    BILL 6
    JACK 4
    JACK 4
    KEN 2
    KEN 2
    MILO 1

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by nicz888 View Post
    Code:
    	int cnt = 0;
    	for (int i =0; i <len; i++)
    	{
    		for (int j = ++i; j<len; j++)
    		{
    			if (strcmp(array[i],array[j]))
    				++cnt;
    		}
    		cout<<array[i]<<" "<<cnt<<endl;
    		cnt =1;
    	}			
    		
    }
    1. The variable i is going up by two each time (one increment in the for loop on i, one increment in the for loop on j), so the comparisons are occuring on elements number 1, 3, 5, 7, etc....
    2. Notice that strcmp returns 0 when the strings are equal, so you're actually finding the number of strings that don't match after the start (sort of, see ...)
    3. You should decide whether cnt needs to start at 0 or at 1.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    i change it to this
    Code:
    	int cnt = 1;
    	for (int i =0; i <len; i++)
    	{
    		for (int j = i+1; j<len; j++)
    		{
    			if (!strcmp(array[i],array[j]))
    				++cnt;
    		}
    		cout<<array[i]<<" "<<cnt<<endl;
    		cnt =1;
    	}			
    		
    }
    my output is
    BILL 3
    BILL 2
    BILL 1
    JACK 2
    JACK 1
    KEN 1
    KEN 2
    MILO 1

    now how do i prints the same name and count just once.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And how do you think you should work around the problem that once you've counted the number of BILL, you count BILL's again (but with one less to count, so you end up with 2 instead of three. [Which is really easy if they are nicely in bunched together, but if the list is "BILL", "JOHN", "BILL", "TOM", "BILL" you may have a bit more of a challenge].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    >>void checkDouble(char array[100][20], int len)

    Correct me if I'm wrong, but I'm pretty sure that you can't do that. You have to have one of the parameters as a char*.

    A vector of strings would be much easier...

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,749
    > Correct me if I'm wrong
    The syntax is fine.

    If you want a * in there, it would be
    void checkDouble(char (*array)[20], int len)

    This also works
    void checkDouble(char array[ ][20], int len)


    What it isn't is either of these:
    void checkDouble(char **array, int len)
    void checkDouble(char *array[20], int len)

    Whilst it's possible to have functions declared like that, neither is suitable for passing a 2D array to. Arrays of pointers perhaps, but not arrays of arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,891
    But this is C++, so why use chars and not std::string (and maybe std::vector)?
    But also for your error, you realize that this is an implementation problem, and not a bug, yes?
    You see, there are two problems - first, if there's a duplicate, like Bill in index 0 and 1, then Bill will be checked for duplicates more than once.
    And two, you are only searching from i + 1, so you aren't checking the entire array again for duplicates. This one may be a bug. You want to check from the beginning and skip checking the index you're comparing to (i), since it would give you a duplicate.
    Last edited by Elysia; 12-20-2007 at 11:16 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21