96 lines
3.6 KiB
C
Executable File
96 lines
3.6 KiB
C
Executable File
/*
|
|
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
|
|
} |