简体   繁体   English

数组中最短和最长的字符串

[英]Shortest and longest string in array

I have an assignment, I need to create a method that receives (char * * ch,site_t size) .我有一个任务,我需要创建一个接收(char * * ch,site_t size)的方法。

ch is an array of addresses to char arrays, I need to make it so that the shortest element will be first (address and place) and longest will be last one (address and place). chchar arrays 的地址数组,我需要让它最短的元素在第一个(地址和位置),最长的元素在最后一个(地址和位置)。 Here is what made so far although it doesn't work on array size 5 (tried only on size 4):这是迄今为止所做的,尽管它不适用于数组大小 5(仅在大小 4 上尝试过):

(Note: I used char * arr[] but I planned to changing it once I get the program working with this type of variable.) (注意:我使用了char * arr[]但我计划在程序使用这种类型的变量时更改它。)

void AdressSwitcher(char * arr[],size_t size){ 
char*shortest=arr[0];
char*shortestFollower=NULL;
char*longest=arr[1];
char*longestFollower=NULL;
 for(size_t i=0;i<size;i++){
      if(strlen(arr[i])<(strlen(shortest))){
        shortest=arr[i];
        arr[i]=arr[0];
      }
      arr[0]=shortest;
}
  for(size_t i=1;i<size;i++){
      if(strlen(arr[i])>(strlen(longest))){
        longest=arr[i];
         arr[i]=arr[size-1];
     }
        arr[size-1]=longest;

// }
 for(size_t i=0;i<size;i++){
   printf("%s %p", arr[i],arr[i]);
   printf("\n");
 }
}

Welcome to SO.欢迎来到 SO。 This problem can be easily solved with the following method:这个问题可以通过以下方法轻松解决:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *escape_double_quotes(const char *s)
{
        char *result = calloc((strlen(s) * 2) + 1, sizeof(char));
        size_t resultIndex = 0;

        for (size_t i = 0; s[i] != '\0'; i++)
        {
                if (s[i] == '"')
                {
                        result[resultIndex] = '\\';
                        resultIndex++;
                        result[resultIndex] = '"';
                        resultIndex++;
                        continue;
                }
                result[resultIndex] = s[i];
                resultIndex++;
        }
        return result;
}

void longestAndShortest(char **arr, const size_t size)
{
        if (size <= 1)
                return;

        size_t shortIndex = 0;
        size_t shortSize = strlen(arr[0]);

        size_t longIndex;
        size_t longSize = 0;

        for (size_t i = 0; i < size; i++)
        {
                size_t b = strlen(arr[i]);

                if (b > longSize)
                {
                        longIndex = i;
                        longSize = b;
                }
                if (b < shortSize)
                {
                        shortIndex = i;
                        shortSize = b;
                }
        }
        printf("The shortest size of the array was %lu, the index of that being %lu and the contents of that being \"%s\".\n", shortSize, shortIndex, escape_double_quotes(arr[shortIndex]));
        printf("The longest size of the array was %lu, the index of that being %lu and the contents of that being \"%s\".\n", longSize, longIndex, escape_double_quotes(arr[longIndex]));

        return;
}

int main(void)
{
        char **array = malloc(sizeof(char*) * 8);

        array[0] = malloc(128);
        strcpy(array[0], "World!");

        array[1] = malloc(128);
        strcpy(array[1], "Hello");

        longestAndShortest(array, 2);

        return 0;
}

From here you should be able to complete the rest.从这里您应该能够完成 rest。

Please work on writing more tidy code.请努力编写更整洁的代码。 Your future self will thank you.你未来的自己会感谢你。 Have a great day!祝你有美好的一天!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM