Informatics3-2024/Solutions3
A MathWikiből
Solutions
Only the solutions we discussed on the practical will be here. All the other tasks are perfect to practice for the written exam.
Character count
#include<stdio.h> int count(char w[], char c) { int i; int s = 0; for(i = 0; w[i] != '\0'; i++) { if(w[i] == c) { s++; } } return s; } int main(void) { char c = 'l'; char str[] = "paprika"; printf("The character %c appears %d times in the string %s.\n", c, count(str, c), str); return 0; }
Most common character
#include<stdio.h> int count(char w[], char c) { int i; int s = 0; for(i = 0; w[i] != '\0'; i++) { if(w[i] == c) { s++; } } return s; } char common(char s[]) { int i; int n = 0; char comm = s[0]; for(i = 0; w[i] != '\0'; i++) { if(count(s, s[i]) > n) { comm = s[i]; n = count(s, s[i]); } } return comm; } int main(void) { char c = 'l'; char str[] = "paprika"; printf("The character %c appears %d times in the string %s.\n", c, count(str, c), str); return 0; }