C 字符串排序 SDUT

Time Limit: 1000 ms Memory Limit: 65536 KiB


Problem Description

输入3个字符串,按字典序从小到大进行排序。


Input

输入数据有一行,分别为3个字符串,用空格分隔,每个字符串长度不超过100。


Output

输出排序后的三个字符串,用空格分隔。


Sample Input

abcd cdef bcde


Sample Output

abcd bcde cdef


Hint

Source


本题可以考虑三个整数排序的方法,只是在交换时用strcpy实现

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
   int main()
   {
       char a[101],b[101],c[101],s[101];

       scanf("%s%s%s",a,b,c);
       if(strcmp(a,b)>0)
       {
           strcpy(s,a);
           strcpy(a,b);
           strcpy(b,s);
       }
       if(strcmp(a,c)>0)
       {
           strcpy(s,a);
           strcpy(a,c);
           strcpy(c,s);
       }
       if(strcmp(b,c)>0)
       {
           strcpy(s,b);
           strcpy(b,c);
           strcpy(c,s);
       }
       printf("%s %s %s",a,b,c);


       return 0;

   }


### 山东理工大学 SDUT C语言 实验9 字符串处理 示例代码解释 #### 输出指定字符的个数 在字符串处理方面,一个常见的操作是统计特定字符在一个给定字符串中的出现次数。下面是一个简单的例子来实现这一功能: ```c #include <stdio.h> #include <string.h> int count_char(const char *str, char ch) { int count = 0; while (*str != '\0') { if (*str == ch) { ++count; } str++; } return count; } int main() { const char string[] = "This is a test."; char targetChar = 't'; printf("The character '%c' appears %d times.\n", targetChar, count_char(string, targetChar)); return 0; } ``` 此段代码定义了一个名为 `count_char` 的辅助函数用于遍历输入字符串并计数目标字符的数量[^1]。 #### 反转字符串 另一个重要的字符串处理技巧是对字符串进行反转。这里展示如何编写一段可以完成该任务的小程序: ```c void reverse_string(char* str) { int length = strlen(str); for (int i = 0; i < length / 2; ++i) { char temp = str[i]; str[i] = str[length - i - 1]; str[length - i - 1] = temp; } } int main() { char inputString[100]; // 假设最大长度不超过100 printf("Enter a string to be reversed:\n"); fgets(inputString, sizeof(inputString), stdin); size_t len = strlen(inputString); if (len > 0 && inputString[len - 1] == '\n') inputString[--len] = '\0'; // 移除fgets读取到的新行符 reverse_string(inputString); printf("Reversed String: %s\n", inputString); return 0; } ``` 这段代码展示了如何通过交换首尾字符逐步向中心靠拢的方式来翻转整个字符串[^2]。 #### 查找子串位置 最后,在实际应用中经常需要用到定位某个模式串首次出现在源串的位置的功能。以下是利用标准库函数 `strstr()` 来达成目的的方法之一: ```c #include <stdio.h> #include <string.h> int find_substring_index(const char* haystack, const char* needle) { const char* found = strstr(haystack, needle); if (!found) return -1; return found - haystack; } int main() { const char sourceStr[] = "hello world"; const char patternStr[] = "world"; int index = find_substring_index(sourceStr, patternStr); if (index >= 0) printf("Substring \"%s\" starts at position %d in the original string.\n", patternStr, index); else printf("Pattern not found!\n"); return 0; } ``` 上述代码片段说明了怎样借助于C语言的标准库函数 `strstr()` 完成子串匹配的任务,并返回其起始索引值[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碧羽o(* ̄▽ ̄*)ブ回雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值