Question

Write the C-code for a function which returns the least common character in an set of...

Write the C-code for a function which returns the least common character in an set of characters that occurs at least once. (The function's prototype is char least_common(int, unsigned char*);)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<conio.h>
#include<stdio.h>


unsigned char least_common(unsigned char *s, int size){
   int i;
   int freq[26] = {0};
   int min = size, minIndex = -1;
   char ch;
   for (i=0;i<size;i++){
       ch = s[i];
       if(ch >= 'a' && ch<='z'){
             freq[ch-'a']++;
       }
       else if( ch >= 'A' && ch<='Z'){
             freq[ch-'A']++;
       }
   }
   for(i = 0;i<26;i++){
       if(min> freq[i] && freq[i] > 0){
           minIndex = i;
           min = freq[i];
       }
   }
   return 'a'+minIndex;
}

int main(int argc, char *argv[]) {
   printf("Most frequent = %c\n",least_common("testString",10));
   getch();
   return 0;
}

Most frequent e Process exited after 5.237 seconds with returnvalue 0 Press any key to continue - - -

\color{red}Please\; upvote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
Write the C-code for a function which returns the least common character in an set of...
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 C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[],...

    Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[], char destination[], int n); This function copies string source to string destination. Parameter n represents the size of array destination. If the latter array is not sufficiently large to hold the whole source string then only the prefix of the string which has room in the latter array should be copied. Note that after copying, the null character should also be included to mark...

  • Write a function to show a character's description. write a function that takes an unsigned character...

    Write a function to show a character's description. write a function that takes an unsigned character c as a parameter and prints c's description. The description of 'a' is just a. But the description of '\n' (the newline character) should be \n, or some other desriptive name, such as newline. Use descriptive names for the tab ('\t') and space characters as well. For an unprintable character whose integer code is m, use description \m. For example, the character with code...

  • using C: Write code for a function with the following prototype: /* * Mask with least...

    using C: Write code for a function with the following prototype: /* * Mask with least signficant n bits set to 1 * Examples: n = 6 --> 0x3F, n = 17 --> 0x1FFFF * Assume 1 <= n <= w */ int lower_one_mask(int n); Your function should follow the bit-level integer coding rules. Be careful of the case n =w.

  • Create a C project named login_l06t1. Write and test a function strnclean that takes two strings...

    Create a C project named login_l06t1. Write and test a function strnclean that takes two strings as parameters, target and source. Using the ctype library, copy only the alphabetic characters from source to target, and make the characters lower case. Ex: source: David Brown! target: davidbrown Prototype: void strnclean(char *target, const char *source); How do i do this using the C type library below! int isalnum(int c) Returns non-zero (true) if c is an alphanumeric character, zero otherwise. int isalpha(int...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • Please complete the following problem in C. No source code is provided. The left-shift operator can...

    Please complete the following problem in C. No source code is provided. The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned int variable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Write a function called smallestLetter that takes in a string argument and returns a char. The...

    Write a function called smallestLetter that takes in a string argument and returns a char. The char that is returned by this function is the character in the string with the lowest ASCII integer code. For example: smallestLetter("Hello") returns ‘H’ which is code 72, and smallestLetter("Hello World") returns ‘ ’ (The space character) which is code 32

  • Assignment 1) Your function will only write the Nth character of the array, supplied as a...

    Assignment 1) Your function will only write the Nth character of the array, supplied as a parameter by the caller. This is an example prototype (although you may want to change the return value type, based on the second twist, below). void writeArrayNthBackward(const char [], int, int, int); The first three arguments serve the same purpose as in the iterative example. The fourth argument (an int) supplies the N for the Nth character to print in the array. If n...

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