用DevC写:输入十个字符串,按非降序排列,并输出排序后的字符串。
时间: 2024-05-12 20:17:21 浏览: 9
好的,我可以为您提供帮助。以下是使用DevC按非降序排列输入十个字符串并输出排序后的代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[10][20], temp[20];
int i, j;
printf("请输入10个字符串:\n");
for(i=0; i<10; i++){
scanf("%s", str[i]);
}
for(i=0; i<9; i++){
for(j=i+1; j<10; j++){
if(strcmp(str[i], str[j])>0){
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
printf("排序后的字符串为:\n");
for(i=0; i<10; i++){
printf("%s\n", str[i]);
}
return 0;
}
```
请尝试运行以上代码,并按照要求输入10个字符串进行排序。如果还有其他问题需要帮助,请继续提问。
相关问题
用DevC++编程实现:有三个字符串:“Welcome”、“come”、“to”、“ China!”,请连成一句话。
### 回答1:
可以使用下面的 DevC 代码来连接三个字符串:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Welcome";
char str2[] = "come";
char str3[] = "to";
char str4[] = " China!";
// 计算所有字符串的总长度
int len = strlen(str1) + strlen(str2) + strlen(str3) + strlen(str4);
// 分配足够大小的内存来存储连接后的字符串
char *result = (char*)malloc(len + 1);
// 将所有字符串连接在一起
strcpy(result, str1);
strcat(result, " ");
strcat(result, str2);
strcat(result, " ");
strcat(result, str3);
strcat(result, " ");
strcat(result, str4);
printf("连接后的字符串: %s\n", result);
return 0;
}
输出结果如下:
连接后的字符串: Welcome come to China!
### 回答2:
在使用DevC进行编程实现时,可以通过以下代码将三个字符串连成一句话:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Welcome";
char str2[] = "come";
char str3[] = "to";
char str4[] = " China!";
char result[30];
strcpy(result, str1);
strcat(result, " ");
strcat(result, str2);
strcat(result, " ");
strcat(result, str3);
strcat(result, str4);
printf("%s", result);
return 0;
}
```
以上代码首先通过`strcpy`将第一个字符串`str1`复制到结果字符串`result`中,然后通过`strcat`函数依次将后续字符串连接到`result`字符串的末尾。最后,使用`printf`函数将结果打印出来。
运行以上代码,输出结果为:
```
Welcome come to China!
```
这便是将三个字符串连成一句话的实现。
### 回答3:
在使用DevC编程实现连接三个字符串时,我们可以使用字符串拼接的方法。首先,我们定义一个字符串变量,用于存储拼接后的结果。然后,将三个字符串依次使用字符串拼接的函数连接起来。
具体步骤如下:
1. 在程序中定义一个字符数组变量,用于存储拼接后的结果,长度应为三个字符串长度之和加1(用于存储字符串末尾的'\0')。
2. 使用strcpy函数将第一个字符串“Welcome”复制到结果字符串中。
3. 使用strcat函数将第二个字符串“come”连接到结果字符串中。
4. 使用strcat函数将第三个字符串“to”连接到结果字符串中。
5. 使用strcat函数将最后一个字符串“ China!”连接到结果字符串中。
6. 最后,输出结果字符串即可。
下面是使用DevC编程实现该功能的示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char result[20];
strcpy(result, "Welcome");
strcat(result, "come");
strcat(result, "to");
strcat(result, " China!");
printf("拼接后的字符串为:%s\n", result);
return 0;
}
```
运行程序后,输出结果为:
拼接后的字符串为:Welcomecometo China!
以上就是使用DevC编程实现将三个字符串连成一句话的方法。
用DEVC++写:输入一行英文单词序列,单词是指连续不含空格的字符串,各单词之间用空格分开,统计其中单词的个数。空格数可以是多个。
这个问题可以使用 DEVC 提供的字符串操作函数来实现。具体的方法是:
1. 从标准输入中读入一行字符串(可以使用 `fgets()` 函数)。
2. 使用 `strtok()` 函数将字符串按照空格分割成若干个单词。
3. 统计单词的个数即可。
下面是一个示例程序:
```c
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1024
int main() {
char str[MAX_LEN];
fgets(str, MAX_LEN, stdin);
int cnt = 0;
char* p = strtok(str, " ");
while (p != NULL) {
cnt++;
p = strtok(NULL, " ");
}
printf("单词个数为 %d\n", cnt);
return 0;
}
```
这个程序读入一行字符串,使用 `strtok()` 函数以空格为分隔符将其分割成单词,然后统计单词的个数输出。注意,需要用 `fgets()` 函数代替 `scanf()` 函数来读入字符串,因为 `fgets()` 可以读入一整行字符串,避免了单词太多导致字符串溢出的问题。