Make a program that filters a list of strings and returns a list with only your friends name in it. If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not... Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"]
时间: 2024-03-31 15:33:55 浏览: 25
Sure, I can help you with that! Here's a Python program that filters a list of strings and returns only your friends' names:
```
def filter_friends(names):
friends = []
for name in names:
if len(name) == 4:
friends.append(name)
return friends
```
You can use this function by passing in a list of names as an argument, like this:
```
names = ["Ryan", "Kieran", "Jason", "Yous"]
friends = filter_friends(names)
print(friends)
```
The output will be:
```
["Ryan", "Yous"]
```
This is because "Ryan" and "Yous" both have exactly 4 letters, so they are your friends! "Kieran" and "Jason" have more than 4 letters, so they are not your friends.
相关问题
Make a JavaScript program that filters a list of strings and returns a list with only your friends name in it. If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not... Ex: Input = ["Ryan", "Kieran", "Jason", "Yous"], Output = ["Ryan", "Yous"]
Of course, here's a JavaScript program that filters a list of strings and returns only your friends' names:
```
function filterFriends(names) {
let friends = [];
for (let i = 0; i < names.length; i++) {
if (names[i].length === 4) {
friends.push(names[i]);
}
}
return friends;
}
```
You can use this function by passing in a list of names as an argument, like this:
```
let names = ["Ryan", "Kieran", "Jason", "Yous"];
let friends = filterFriends(names);
console.log(friends);
```
The output will be:
```
["Ryan", "Yous"]
```
This is because "Ryan" and "Yous" both have exactly 4 letters, so they are your friends! "Kieran" and "Jason" have more than 4 letters, so they are not your friends.
Write a program to prompt the user to enter a set of strings and the program would sort and display them in ascending alphabetical order. It is accepted to use the functions in the string.h library in the program.
Sure, here's a C program that prompts the user to enter a set of strings, sorts them using the `qsort()` function from the `stdlib.h` library, and then displays them in ascending alphabetical order:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STRINGS 100
#define MAX_STRING_LENGTH 100
int compare_strings(const void* a, const void* b) {
return strcmp(*(const char**)a, *(const char**)b);
}
int main() {
char strings[MAX_STRINGS][MAX_STRING_LENGTH];
int num_strings = 0;
printf("Enter a set of strings (up to %d, one per line, press Ctrl+D to stop):\n", MAX_STRINGS);
// Read in the strings
while (num_strings < MAX_STRINGS && fgets(strings[num_strings], MAX_STRING_LENGTH, stdin)) {
// Remove the newline character at the end
strings[num_strings][strcspn(strings[num_strings], "\n")] = '\0';
num_strings++;
}
// Sort the strings
qsort(strings, num_strings, sizeof(char[MAX_STRING_LENGTH]), compare_strings);
// Display the sorted strings
printf("Sorted strings:\n");
for (int i = 0; i < num_strings; i++) {
printf("%s\n", strings[i]);
}
return 0;
}
```
In this program, the `compare_strings()` function is used as the comparison function for `qsort()`, which takes two `const void*` arguments and returns an integer indicating the relative order of the two elements. The `main()` function prompts the user to enter a set of strings, reads them in using `fgets()`, sorts them using `qsort()`, and then displays them using `printf()`.
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)