DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2005
    Posts
    45

    Check duplicated characters in a String

    Hi,

    Assume that I have a String and I want to check whether there is one (or more)
    character that appear more than once in that String. How should I do that? I'm a bit stuck at the algorithm. Please help.

    Thanks.

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    An algorithm:
    Use a hashtable to save key=char, value = count
    Loop thru length of string
    Get next char from the string
    Look in hashtable for the char
    If not found, create a new entry for that char with value = 1
    if found, increment value by 1
    end loop
    get keys from hashtable and retrieve values

  3. #3
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    10
    Code:
    //start for or while loop, and nest another on inside
    for(int i = 0; i < stringName.length(); i++) {
        for(int j = 0; j <stringName.length(); j++) {
             /*compare the character srting's character position i with j using .charAt(int place) method, if they are the same add 1 to a counter. At the end of the for(j) loop decide if the counter is above 2. if it is then you have a repeated letter */
            if(compare two characters at i and j in stringName and check if same) {
                  add one to a counter;
            }
         }
          if(counter > 1) {
             do what you want to do once you have found two of the same characters in the same string.
         }
    }
    I prefer to live in my own shadow. At least then I can be compared to a false past I'm more familiar with

Similar Threads

  1. replacing characters in string
    By Reuben in forum Enterprise
    Replies: 1
    Last Post: 10-26-2000, 02:43 AM
  2. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 07:36 AM
  3. Replies: 0
    Last Post: 06-22-2000, 07:30 AM
  4. NetUserEnum Api
    By Mike in forum VB Classic
    Replies: 0
    Last Post: 04-24-2000, 03:05 PM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links