Question

Write a function that would accept a string and checks if any character is repeated more...

Write a function that would accept a string and checks if any character is repeated more than once. The function would return the repeated chars and the number of times they are repeated. The return value is a string, all pairs separated by a comma.

E.G. function(“2abc**zac22”); returns: 2=3,a=2,c=2,*=2

0 0
Add a comment Improve this question Transcribed image text
Answer #1
function getCounts(s){
    var values = [];
    var counts = [];
    for(var i = 0;i<s.length;i++){
        var f = 0;
        var j = 0;
        while(j<values.length){
            if(values[j]==s[i]){
                f = 1;
                break;
            }
            j++;
        }
        if(f == 0){
            values[values.length] = s[i];
            counts[counts.length] = 1;
        }
        else{
            counts[j] += 1;

        }
    }
    var result = "";
    for(var k = 0;k<values.length;k++){
        if(counts[k]>1){
            if(k == 0)
                result += (values[k]+"="+counts[k])
            else
                result += (","+values[k]+"="+counts[k])
        }
    }
   return result;
}

Add a comment
Know the answer?
Add Answer to:
Write a function that would accept a string and checks if any character is repeated more...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • 2) Write a function stringManip that takes in a character string and returns a character string...

    2) Write a function stringManip that takes in a character string and returns a character string following these rules: a) any vowel (a, e, i, o, u) is replaced by the character '&' b) any numeric character has 1 added to it (if it was 9, it becomes O instead) (str2num() is useful here) c) all lowercase characters become uppercase d) replace the final character with '!' e) append the length of the string before this step to the end...

  • Write a functionthat calculates the first character in a string that exists more than twice in...

    Write a functionthat calculates the first character in a string that exists more than twice in the stringand returns the index of that character or -1 if none exists.The program should prompt the user to enter an entire line including any whitespace as a string. The program then prints out the index and character that exists at least three times or a message “Not found” if all characters of the string have none or single duplicates. In C++ please For...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”)...

    Write a recursive function to convert a character string of digits to an integer. Example: convert(“1234”) returns 1234. Hint: To convert a character to a number, subtract the ASCII value ‘0’ from the character. For example, if the string s has only one character, then the function can return the value s[0] – ‘0’.

  • Python Write a function named letter_function. The function should accept a string (i.e., any string) and...

    Python Write a function named letter_function. The function should accept a string (i.e., any string) and display the letters in that string. For example, if the function received "python 3.2". The following letters will be displayed: (Hint: you can use s.isalpha() to know if a character is a letter) p y t h o n

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • Python File Compression: Write a function called “compressString” which compresses a string such that any consecutive...

    Python File Compression: Write a function called “compressString” which compresses a string such that any consecutive repeated characters are replaced with one of the character and the number of times the character is repeated. This is also known as "Run length encoding". Examples:“aaabbc” -> “a3b2c”“abc” -> “abc”“aaqakkaccc” -> a2qak2a2c3

  • write a function which takes a string argument and a character argument. It should return a...

    write a function which takes a string argument and a character argument. It should return a truth value (int 0 or 1), 0 if the string does not contain the character, and 1 if the string does contain the character. Do not use a built-in library for this. Again, call this function and its variables whatever you deem appropriate. The main function of the program should accept a single character followed by Enter. Then, it will read lines until the...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT