added weekly exercises of isp-ws25
This commit is contained in:
96
c/isp-ws25/weeklies/week05/w05ex01.c
Executable file
96
c/isp-ws25/weeklies/week05/w05ex01.c
Executable file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
In this task, you should implement three fundamental string functions yourself.
|
||||
|
||||
Your program should read two words from the user (up to 25 characters each) and then work with these two strings. Since fgets() also stores the newline character ('\n') when the user presses Enter, even an "empty" input will have a length of 1. After implementing the three functions described below, the program should print the length of each word, check whether they are equal, and print a copy of the first word.
|
||||
|
||||
int stringLength(char* string): This function should count how many characters appear in the string before the terminating '\0'. Use a loop that increments a counter until you reach the end of the string. The function should return this length as an integer but should not print anything itself. Remember that fgets() keeps the newline character ('\n') when the user presses Enter, so it will also be included in the length.
|
||||
|
||||
int stringCompare(char* first_string, char* second_string): This function should compare two strings character by character and return 0 if the strings are exactly the same. If the strings are different, it should return a negative value if the first differing character in first_string has a lower ASCII value than the corresponding character in second_string, and a positive value if it has a higher ASCII value. The comparison is case-sensitive, so uppercase and lowercase letters are treated as different. The function should not print anything. The output (Words are equal or Words are not equal) is handled in main().
|
||||
|
||||
void stringCopy(char* from, char* to): This function should copy the content of one string into another. Copy every character from from to to until the null terminator is reached, and make sure to also copy the terminating '\0'. The function does not return a value and does not print anything. The copied word is printed in main().
|
||||
|
||||
The main() function is already provided. You are not allowed to use <string.h>
|
||||
|
||||
This is an example:
|
||||
|
||||
Enter first word: Hello
|
||||
Enter second word: hello
|
||||
Length of first word: 6
|
||||
Length of second word: 6
|
||||
Words are not equal
|
||||
Copied word: Hello
|
||||
*/
|
||||
#include <stdio.h>
|
||||
|
||||
#define SIZE 26
|
||||
|
||||
int stringLength(char* string);
|
||||
int stringCompare(char* first_string, char* second_string);
|
||||
void stringCopy(char* from, char* to);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char first_word[SIZE];
|
||||
char second_word[SIZE];
|
||||
char copy[SIZE];
|
||||
|
||||
printf("Enter first word: ");
|
||||
fgets(first_word, SIZE, stdin);
|
||||
printf("Enter second word: ");
|
||||
fgets(second_word, SIZE, stdin);
|
||||
|
||||
int first_word_length = stringLength(first_word);
|
||||
int second_word_length = stringLength(second_word);
|
||||
printf("Length of first word: %d\n", first_word_length);
|
||||
printf("Length of second word: %d\n", second_word_length);
|
||||
|
||||
if (stringCompare(first_word, second_word) == 0)
|
||||
{
|
||||
printf("Words are equal\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Words are not equal\n");
|
||||
}
|
||||
|
||||
stringCopy(first_word, copy);
|
||||
printf("Copied word: %s\n", copy);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stringLength(char* string)
|
||||
{
|
||||
int string_length = 0;
|
||||
|
||||
while (string[string_length] != '\0')
|
||||
{
|
||||
//printf("char is %c\n", string[string_length]);
|
||||
string_length++;
|
||||
}
|
||||
return string_length;
|
||||
}
|
||||
|
||||
int stringCompare(char* first_string, char* second_string)
|
||||
{
|
||||
int string_length = 0;
|
||||
|
||||
while (first_string[string_length] != '\n')
|
||||
{
|
||||
if(first_string[string_length] != second_string[string_length]) { return 1; }
|
||||
string_length++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stringCopy(char* from, char* to)
|
||||
{
|
||||
int string_length = 0;
|
||||
|
||||
while (from[string_length] != '\n')
|
||||
{
|
||||
to[string_length] = from[string_length];
|
||||
string_length++;
|
||||
}
|
||||
// TODO: copy the content from one string to another
|
||||
}
|
||||
72
c/isp-ws25/weeklies/week05/w05ex02.c
Executable file
72
c/isp-ws25/weeklies/week05/w05ex02.c
Executable file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
In this task, you should write a program that analyses sentences provided by the user. In the first step, you have to implement the user input in the main using fgets (see TODO in the main function). You can assume that the user will never enter more than 200 characters. Then, you should implement two functions:
|
||||
|
||||
countVowel(char string[]): This function counts the vowels (de: Vokale) in the string and returns it as an integer. The return value (number of vowels) is then printed in the provided main() function.
|
||||
printReverse(char string[]): This function prints the provided string in reverse and adds a \n at the end. It has no return value (void). Make sure to avoid printing the string’s null terminator. If the string is empty (no characters, just the user pressing ‘Enter’/’Return’) the function should print Empty string\n and end.
|
||||
|
||||
You are allowed to use any library, including <string.h>.
|
||||
|
||||
This is an example (once the code is fixed):
|
||||
|
||||
Enter sentence (max 200 chars): Hello Students
|
||||
number of vowels: 4
|
||||
|
||||
stnedutS olleH
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// TODO: write your functions
|
||||
int countVowel(char* string);
|
||||
void printReverse(char* string);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char my_string[200] = {0};
|
||||
printf("Enter sentence (max 200 chars): ");
|
||||
fgets(my_string,200,stdin);
|
||||
|
||||
int nr_vowel = countVowel(my_string);
|
||||
printf("number of vowels: %d\n", nr_vowel);
|
||||
printReverse(my_string);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int countVowel(char* string)
|
||||
{
|
||||
int position = 0;
|
||||
int vowels = 0;
|
||||
|
||||
while (string[position] != '\0')
|
||||
{
|
||||
if (
|
||||
string[position] == 'a' ||
|
||||
string[position] == 'e' ||
|
||||
string[position] == 'i' ||
|
||||
string[position] == 'o' ||
|
||||
string[position] == 'u' ||
|
||||
string[position] == 'A' ||
|
||||
string[position] == 'E' ||
|
||||
string[position] == 'I' ||
|
||||
string[position] == 'O' ||
|
||||
string[position] == 'U'
|
||||
) { vowels++; }
|
||||
position++;
|
||||
}
|
||||
return vowels;
|
||||
}
|
||||
|
||||
void printReverse(char* string)
|
||||
{
|
||||
int position = 0;
|
||||
while (string[position] != '\n') { position++; }
|
||||
if (position == 0) { printf("Empty string\n"); return; }
|
||||
while (position >= 0)
|
||||
{
|
||||
printf("%c",string[position]);
|
||||
position--;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
52
c/isp-ws25/weeklies/week05/w05ex03.c
Executable file
52
c/isp-ws25/weeklies/week05/w05ex03.c
Executable file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
In this task, you should implement a function that swaps two elements in an existing array. The main function is already predefined. Your task is:
|
||||
|
||||
Add the user input to the main function by reading the first and second index from user input and write it into the array indices-array. indices[0] should contain the first user input. indices[1] should contain the second user input. You can use scanf for this operation. The two user inputs define the indices of the two elements that should be switched in the array.
|
||||
Write a function called swapTwoNumbers(). The function takes 3 arguments: i) the array of numbers, ii) the size of the array as int, and iii) the array where the two indices to swap are stored. The function should check if the indices are valid in the current array. If one or both indices are invalid the function should return 1. Otherwise the elements in the array should be swapped and the function should return 0.
|
||||
|
||||
The provided main function prints the array or an error message, if the user enters invalid indices.
|
||||
|
||||
This is an example:
|
||||
|
||||
0
|
||||
4
|
||||
5 2 3 4 1
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#define SIZE 5
|
||||
|
||||
// TODO: add your function swapTwoNumbers()
|
||||
int swapTwoNumbers(int array[], int size, int indices[]);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int my_array[SIZE] = {1,2,3,4,5};
|
||||
int size = sizeof(my_array) / sizeof(my_array[0]);
|
||||
|
||||
|
||||
int indices[2] = {-1, -1};
|
||||
scanf("%d",&indices[0]);
|
||||
scanf("%d",&indices[1]);
|
||||
|
||||
int failed = swapTwoNumbers(my_array, size, indices);
|
||||
if (failed)
|
||||
{
|
||||
printf("Indices out of range\n");
|
||||
return 0;
|
||||
}
|
||||
for (int i = 0; i < SIZE; i++)
|
||||
{
|
||||
printf("%d ", my_array[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int swapTwoNumbers(int array[], int size, int indices[])
|
||||
{
|
||||
if (indices[1] >= size){return 1;}
|
||||
int placeholder = array[indices[0]];
|
||||
array[indices[0]] = array[indices[1]];
|
||||
array[indices[1]] = placeholder;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user