鱼C论坛

 找回密码
 立即注册
查看: 433|回复: 2

c语言文件

[复制链接]
发表于 2024-10-30 16:28:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
长度为N的字符串s,按照把第i个字符放到第i-1个字符末尾的规律分解成n个字符串例:China 字符串长度为5,分解为以下5个字符串:china , hinac,inach, nachi,achin。通过比较字符串的首字符将N个字符串进行升序排序。如果首字符相同,按照在s中的位置从小到大排序。再将N个字符串的首字符放在新的字符串s1 中的并输出字符串 s1。求原字符串s的末尾字符在s1中的位置并输出,输入格式:s1字符串写入到 file.txt 文件中
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-10-30 16:33:41 | 显示全部楼层
要实现这个功能,我们可以用C语言编写一个程序来完成字符串的分解、排序和输出。以下是实现步骤的简要说明:

1. **分解字符串**:将输入字符串`s`分解成长度为`N`的字符串数组,每个字符串是通过将第`i`个字符放到第`i-1`个字符末尾形成的。

2. **排序字符串**:根据每个字符串的首字符进行升序排序。如果首字符相同,则根据它们在原字符串中的位置排序。

3. **生成新字符串`s1`**:将排序后的字符串的首字符依次放入新的字符串`s1`中。

4. **查找原字符串末尾字符的位置**:在`s1`中查找原字符串`s`的末尾字符的位置。

5. **输出结果**:将`s1`写入到`file.txt`文件中,并输出原字符串末尾字符在`s1`中的位置。

以下是实现该功能的C语言代码示例:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. // 定义一个结构体来存储字符串和它在原字符串中的位置
  5. typedef struct {
  6.     char *str;
  7.     int index;
  8. } StringWithIndex;

  9. // 比较函数,用于qsort排序
  10. int compare(const void *a, const void *b) {
  11.     StringWithIndex *strA = (StringWithIndex *)a;
  12.     StringWithIndex *strB = (StringWithIndex *)b;
  13.     if (strA->str[0] == strB->str[0]) {
  14.         return strA->index - strB->index;
  15.     }
  16.     return strA->str[0] - strB->str[0];
  17. }

  18. int main() {
  19.     char s[] = "China";
  20.     int N = strlen(s);
  21.     StringWithIndex strings[N];

  22.     // 分解字符串
  23.     for (int i = 0; i < N; i++) {
  24.         strings[i].str = (char *)malloc((N + 1) * sizeof(char));
  25.         strcpy(strings[i].str, s + i);
  26.         strncat(strings[i].str, s, i);
  27.         strings[i].index = i;
  28.     }

  29.     // 排序字符串
  30.     qsort(strings, N, sizeof(StringWithIndex), compare);

  31.     // 生成新字符串s1
  32.     char s1[N + 1];
  33.     for (int i = 0; i < N; i++) {
  34.         s1[i] = strings[i].str[0];
  35.     }
  36.     s1[N] = '\0';

  37.     // 查找原字符串末尾字符在s1中的位置
  38.     char lastChar = s[N - 1];
  39.     int position = -1;
  40.     for (int i = 0; i < N; i++) {
  41.         if (s1[i] == lastChar) {
  42.             position = i;
  43.             break;
  44.         }
  45.     }

  46.     // 输出结果
  47.     FILE *file = fopen("file.txt", "w");
  48.     if (file != NULL) {
  49.         fprintf(file, "%s
  50. ", s1);
  51.         fclose(file);
  52.     }
  53.     printf("The position of the last character in s1 is: %d
  54. ", position);

  55.     // 释放内存
  56.     for (int i = 0; i < N; i++) {
  57.         free(strings[i].str);
  58.     }

  59.     return 0;
  60. }
复制代码


这个程序会将生成的`s1`写入到`file.txt`文件中,并输出原字符串末尾字符在`s1`中的位置。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-10-30 16:39:14 | 显示全部楼层
本帖最后由 jackz007 于 2024-10-30 20:53 编辑
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         FILE * fp                                             ;
  6.         char c , s[256][256]                                  ;
  7.         int d[256] , i , j , n , t                            ;
  8.         for(n = 0 ; (c = getchar()) != '\n' ;) {
  9.                 if(c >= 'A' && c <= 'Z') c = c + ' '          ;
  10.                 s[0][n ++] = c                                ;
  11.         }
  12.         s[0][n] = 0                                           ;
  13.         for(i = 1 ; i < n ; i ++) {
  14.                 for(j = 0 ; j < n ; j ++) {
  15.                         s[i][j] = s[0][(i + j) % n]           ;
  16.                 }
  17.                 s[i][j] = 0                                   ;
  18.         }
  19.         for(i = 0 ; i < n ; i ++) d[i] = i                    ;
  20.         for(i = 0 ; i < n - 1 ; i ++) {
  21.                 for(j = i + 1 ; j && strcmp(s[d[j]] , s[d[j - 1]]) < 0 ; j --) {
  22.                         t = d[j - 1]                          ;
  23.                         d[j - 1] = d[j]                       ;
  24.                         d[j] = t                              ;
  25.                 }
  26.         }
  27.         if((fp = fopen("file.txt" , "w"))) {
  28.                 fprintf(fp , "%s\n" , s[0])                   ;
  29.                 for(i = 0 ; i < n ; i ++) {
  30.                         printf("%c" , s[d[i]][0])             ;
  31.                         fprintf(fp , "%c" , s[d[i]][0])       ;
  32.                 }
  33.                 printf("\n")                                  ;
  34.                 fprintf(fp , "\n")                            ;
  35.                 for(i = 0 ; i < n ; i ++) {
  36.                         if(d[i] == n - 1) {
  37.                                 printf("%d\n" , i)            ;
  38.                                 fprintf(fp , "%d\n" , i)      ;
  39.                                 break                         ;
  40.                         }
  41.                 }
  42.                 fclose(fp)                                    ;
  43.         } else {
  44.                 fprintf(stderr , "无法创建文件 [file.txt]\n") ;
  45.         }
  46. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 01:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表