如何使这个功能需要任意字符串? [英] How do I make this function take arbitrary strings?

查看:166
本文介绍了如何使这个功能需要任意字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,基本上,现在,这个功能只能拿9个字,每个10个字符。我如何使它以便它可以采取任意词金额字符和按字母顺序排序,相应的他们?

  INT sortText(){
  炭名称[10] [9],TNAME [10] [9],温度[10];
  INT I,J,N;  的printf(请输入您要排序(最多9)字样的量:);
  scanf函数(%d个,&安培; N);
  的printf(请输入%d个字:N);
  对于(i = 0; I< N;我++)
  {
      scanf函数(%S,名称由[i]);
      的strcpy(TNAME [I],名称[I]);
  }
  对于(i = 0; I< N - 1;我++){
      为(J = I + 1; J< N; J ++){
          如果(STRCMP(名称[I],名称[J])0){
              的strcpy(温度,名称由[i]);
              的strcpy(名称[I],名称[J]);
              的strcpy(名称[J],温度);
          }
      }
  }
  的printf(\\ n ------------------------------------------ \\ n );
  的printf(% - 3%的4S%-11 \\ n,输入,|,输出);
  的printf(------------------------------------------ \\ n);
  对于(i = 0; I< N;我++)
  {
      的printf(%S \\ t \\ t%S \\ n,TNAME [I],名称[I]);
  }
  的printf(------------------------------------------ \\ n);
  }


解决方案

您有两个问题,每个需要单独解决,但是他们仍然可以在类似的办法就是使用来解决,动态内存分配,更重要的再分配

有要记住两个重要方面,第一个是一个字符串的字符数组(有特殊终止字符),并且你可以有一个指针位于存储器中的任何一个数组。

如果我们先从数据类型,你应该如何存储你的字符串,你要的是一个数组的数组,就像你现在所拥有的,而是动态分配的,这意味着你要指针数组(琴弦),但由于字符串的数组也需要动态需要一个指向它含有指向其他阵列的阵列,即一个指针的指针的char **

现在,当我们知道什么样的数据类型来使用,让思考如何分配它。要为您的阵列中的一个字符串分配空间,你分配一个的char * 使用的 的malloc 功能:

 的char **字符串=的malloc(1 * sizeof的(字符*));

这是简单的部分。我们开始阅读的实际字符串现在之前,让我们想想如何将新字符串添加到您的收藏:这是通过的重新分配的字符串数组你有,使用的 的realloc 功能:

 的char ** temp_strings = realloc的(字符串,CURRENT_COUNT + 1 * sizeof的(字符*));
如果(temp_string == NULL)
{
    //分配失败,妥善处理错误
}
字符串= temp_strings;
++ CURRENT_COUNT;

下面的变量 CURRENT_COUNT 是字符串数组的当前长度,应首先初始化为 1 (因为我们只有在数组中的单个字符串)。

现在实际字符串的阅读,这是一个有点复杂,因为我们实际上无法读取整个字符串(因为我们不知道每一行有多长)。相反,我们一次读取一个字符,并结束的时候,我们打了一个换行符。我们还需要重新分配每个字符的字符串。

也许是这样的:

  INT CH;
字符* S = NULL;
为size_t CURRENT_LENGTH = 0; //字符串的当前长度而((C =龟etc(标准输入))!= EOF)
{
    如果(C =='\\ n')
        打破; //换行,与当前字符串进行    如果(S == NULL)
    {
        S = malloc的(2); //分配两个角色:一个用于C,一个用于终止
    }
    其他
    {
        //当前长度不包括终止
        //这就是为什么我们添加两个字符
        字符* temp_s = realloc的(S,CURRENT_LENGTH + 2);
        如果(temp_s == NULL)
        {
            //处理错误
        }
        S = temp_s;
    }    S [CURRENT_LENGTH ++] = C;
    S [CURRENT_LENGTH] ='\\ 0'; //终止作为一个字符串
}如果(S!= NULL)
{
    //字符串添加到字符串数组
    串[CURRENT_COUNT] =秒;
}

So basically, right now, this function can only take 9 words with 10 characters each. How do i make it so that it can take an arbitrary amount of words and characters and sort them accordingly in alphabetical order?

int sortText(){ 
  char name[10][9], tname[10][9], temp[10];
  int i, j, n;

  printf("Enter the amount of words you want to sort (max 9):");
  scanf("%d", &n);
  printf("Enter %d words: ",n);
  for (i = 0; i < n; i++)
  {
      scanf("%s", name[i]);
      strcpy(tname[i], name[i]);
  }
  for (i = 0; i < n - 1 ; i++){
      for (j = i + 1; j < n; j++){
          if (strcmp(name[i], name[j]) > 0){
              strcpy(temp, name[i]);
              strcpy(name[i], name[j]);
              strcpy(name[j], temp);
          }
      }
  }
  printf("\n------------------------------------------\n");
  printf("%-3s %4s %11s\n", "Input","|", "Output");
  printf("------------------------------------------\n");
  for (i = 0; i < n; i++)
  {
      printf("%s\t\t%s\n", tname[i], name[i]);
  }
  printf("------------------------------------------\n");
  }

解决方案

You have two problems, that each needs to be solved separately, but they can still be solved in a similar way, namely using dynamic memory allocations and more importantly reallocation.

There are two important aspects to remember here, and the first is that a string is an array of characters (with a special terminating character) and that you can have a pointer to an array located anywhere in memory.

If we start with the data-types and how you should store your strings, what you want is an array of arrays, much like you have right now, but allocated dynamically which means you want an array of pointers (to the strings), but since the array of string also needs to be dynamic you need a pointer to an array which contains pointers to other arrays, i.e. a pointer to a pointer to char: char **.

Now when we know what data-type to use, lets think about how to allocate it. To allocate space for a single string in your array, you allocate one char * using the malloc function:

char **strings = malloc(1 * sizeof(char *));

That was the simple part. Now before we start reading the actual string, lets think about how to add a new string to your collection: This is done by reallocating the array of strings you have, using the realloc function:

char **temp_strings = realloc(strings, current_count + 1 * sizeof(char *));
if (temp_string == NULL)
{
    // Allocation failed, handle error appropriately
}
strings = temp_strings;
++current_count;

Here the variable current_count is the current length of the array of strings, it should be initially initialized to 1 (as we only have a single string in the array).

Now for the reading of the actual strings, and this is a little more complicated since we actually can't read whole strings (since we don't know how long each line is). Instead we read one character at a time, and end when we hit a newline. We also need to reallocate the string for each character.

Maybe something like this:

int ch;
char *s = NULL;
size_t current_length = 0;  // Current length of string

while ((c = fgetc(stdin)) != EOF)
{
    if (c == '\n')
        break;  // Newline, done with the current string

    if (s == NULL)
    {
        s = malloc(2);  // Allocate two character: One for c and one for the terminator
    }
    else
    {
        // The current length is not including the terminator
        // that's why we add two characters
        char *temp_s = realloc(s, current_length + 2);
        if (temp_s == NULL)
        {
            // Handle error
        }
        s = temp_s;
    }

    s[current_length++] = c;
    s[current_length] = '\0';  // Terminate as a string
}

if (s != NULL)
{
    // "Add" the string to the array of strings
    strings[current_count] = s;
}

这篇关于如何使这个功能需要任意字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆