C code String And Comparison

Joined
Dec 27, 2022
Messages
1
Reaction score
0
Good morning, I have just received the following task and am struggling to find a solution for it: Write a function that receives two strings from the main function. The function must go through the elements one by one of both strings and compare them. If both are letters, it prints the larger among them. If they are both anything else besides letters it does the same. If they differ (letter and something else), a blank space is printed.
This is the assignment I received and am struggling with. Thanks for all of your time.
 
Joined
Dec 21, 2022
Messages
28
Reaction score
4
Maybe you can share with us what you already have and where are the problems?
Also, which programming language shall be used?
 
Joined
Jan 8, 2023
Messages
27
Reaction score
2
C:
#include <stdio.h>
#include <ctype.h>

void compare_strings(char *str1, char *str2) {
  int i = 0;
  while (str1[i] != '\0' && str2[i] != '\0') {
    if (isalpha(str1[i]) && isalpha(str2[i])) {
      // Both characters are letters
      if (str1[i] > str2[i]) {
        printf("%c", str1[i]);
      } else {
        printf("%c", str2[i]);
      }
    } else if (!isalpha(str1[i]) && !isalpha(str2[i])) {
      // Both characters are not letters
      if (str1[i] > str2[i]) {
        printf("%c", str1[i]);
      } else {
        printf("%c", str2[i]);
      }
    } else {
      // Characters are different types (letter and not letter)
      printf(" ");
    }
    i++;
  }
}
  1. The function begins by declaring two pointers to characters, str1 and str2, which will store the addresses of the two strings passed as arguments.
  2. An integer variable i is initialized to 0. This variable will be used as an index to access the characters of the strings.
  3. The function enters a while loop that will run as long as the characters at the current index of both strings are not the null character '\0', which indicates the end of the string.
  4. Within the loop, an if statement checks if the current characters of both strings are letters (using the isalpha function from the ctype.h library). If this is the case, another if statement compares the characters and prints the larger of the two.
  5. If the characters are not letters, another if statement checks if they are both not letters (using the ! operator to negate the result of isalpha). If this is the case, the characters are compared and the larger of the two is printed.
  6. If the characters are of different types (one letter and one not letter), a blank space is printed using printf(" ").
  7. The index i is incremented, and the loop continues until the end of both strings is reached.


hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top