Coding
  • Welcome
  • Baisc Knowledge
    • Vim
    • C
      • Library
    • Java
      • Setup
      • Java Basic
  • Kattis
    • Easy
      • A Second Opinion
      • A Shortcut to What?
      • A Stack of Gold
      • ACM Contest Scoring
      • ASCII kassi
      • Aaah!
      • Add Two Numbers
      • Adding Trouble
      • Afjörmun
      • Airfare Grants
      • Above Average
      • Akcija
      • Alphabet Spam
      • Amerískur vinnustaður
      • Anti-Palindrome
      • Apaxiaaaaaaaaaaaans!
      • Arithmetic Functions
      • Arm Coordination
      • Arrangement
      • Attendance
      • Autori
      • Average Character
      • Avion
      • Baby Bites
      • Babylonian Numbers
      • ABC
      • Aldur
      • Backspace
      • Bannorð
      • Barcelona
      • Basketball One-on-One
      • Batter Up
      • Beavergnaw
      • Bela
      • BergMál
      • Bergur*
      • Akureyri*
      • Best Compromise
      • Best Relay Team*
      • Besta gjöfin
      • Betting
      • Bijele
      • Bilað Lyklaborð
      • Bitte ein Bit
      • Blandað Best
      • Blaðra
      • Blaðra2
      • Bluetooth*
      • Booking a Room
      • Bottle Opening
      • Bounding Robots
      • Breaking Branches*
      • Bracket Matching*
      • Broken Swords
      • Building Pyramids
      • Bus
      • Bus Assignment
      • CPR Number
      • Call for Problems
      • Canadians, eh?
      • Candy Store
      • Cetiri
      • Cetvrta
      • Champernowne Verification
      • Chanukah Challenge
      • Chardonnay
      • Chocolate Division*
      • Chugging
      • Cinema Crowds 2
      • Class Field Trip
      • ASCII Kassi 2
      • Coffee Cup Combo
      • Cold-puter Science
      • Composed Rhythms
      • Cookies
      • Cooking Water
      • Cornhusker
      • Cosmic Path Optimization
      • Count the Vowels
Powered by GitBook
On this page
  • Question
  • Solution
  • Idea
  • Code
Edit on GitHub
  1. Kattis
  2. Easy

Average Character

PreviousAutoriNextAvion

Last updated 6 months ago

Question

Solution

Idea

The basic idea is easy, just to get the average ASCII value of each character in the input string.

However, if C is the language being used, things get a bit tricky regarding the input. Here, we can only use fgets() to read the input because white space characters cannot be ignored in this question. So, the correct way to read the input is below:

#define MAX_LEN 102

if (fgets(string, MAX_LEN, stdin))
{
  string[strcspn(string, "\n")] = 0;
}

Here the MAX_LEN is 2 characters more than 100 (the actual length of the string). This is because:

  1. Add 1 to take the null character into account (the property of string in C)

  2. Add 1 to take the \n character which is read by fgets() (the property of fgets() in C)

Code

https://open.kattis.com/problems/averagecharacteropen.kattis.com
https://github.com/mendax1234/Coding-Problems/blob/main/kattis/averagecharacter/averagecharacter.c
#include <stdio.h>
#include <string.h>

#define MAX_LEN 102

char get_average_ascii(char string[MAX_LEN])
{
  long i = 0;
  long sum = 0;
  while(string[i] != 0)
  {
    sum += (long)string[i];
    i += 1;
  }
  long avg = sum / i;
  return (char)avg;
}

int main()
{
  char string[MAX_LEN];

  if (fgets(string, MAX_LEN, stdin))
  {
    string[strcspn(string, "\n")] = 0;
  }

  putchar(get_average_ascii(string));
}