简体   繁体   English

c-打印最长的字符串和字符串长度?

[英]c - printing the longest string and string length?

I'm new to c programming, and i'm suppose to write a code to find the longest string and it's string length from an array of strings. 我是C语言编程的新手,我想写一个代码来查找最长的字符串,它是字符串数组中的字符串长度。 This is my code so far. 到目前为止,这是我的代码。

#include <stdio.h>
#include <string.h>
#define N 20
char *longestStrInAr(char str[N][40], int size, int *length);
int main()
{
    int i, size, length;
    char str[N][40], first[40], last[40], *p;
    char dummychar;

    printf("Enter array size: \n");
    scanf("%d", &size);
    scanf("%c", &dummychar);
    for (i=0; i<size; i++) {
        printf("Enter string %d: \n", i+1);
        gets(str[i]);
    }
    p = longestStrInAr(str, size, &length);
    printf("longest: %s \nlength: %d\n", p, length);
    return 0;
}
char *longestStrInAr(char str[N][40], int size, int *length)
{
    int i,j=0;
    int len = 0;
    *length = 0;
    char word[N];
    for(i=0;i<size;i++){
        while(str[i][j]!='\0'){
            len++;
            j++;
        }
        if(*length<len){
            *length = len;
            strcpy(word,str[i]);
        }
    }
    return word;
}

The main{} is a given function, and the code under *longestStrInAr is the one im suppose to write. main {}是一个给定的函数,* longestStrInAr下的代码是一个我想写的代码。 However, my current code is only able to give me the longest string length, and prints out (null) instead of the longest string. 但是,我当前的代码只能给我最长的字符串长度,并且打印出(空)而不是最长的字符串。 my output 我的输出

You copy the string into local variable and then return pointer to it. 您将字符串复制到本地变量,然后返回指向它的指针。 But after returning, the variable is no longer valid. 但是返回后,该变量不再有效。 That is undefined behavior and "anything" can happen. 那是不确定的行为,“任何事情”都可能发生。 In this case it seems some optimization or a debug helper turns it to null. 在这种情况下,似乎进行了一些优化或调试助手将其设置为null。

Instead just keep a pointer to the longest string in the str array, don't copy anything and return that pointer. 而是只保留指向str数组中最长字符串的指针,不要复制任何内容并返回该指针。 Alternatively, if you want to simplify it, return the i index of the longest string. 或者,如果要简化它,则返回最长字符串的i索引。

Problems I see: 我看到的问题:

Problem 1 问题1

Don't use gets . 不要使用gets It's a security hole. 这是一个安全漏洞。 Use fgets instead. 请改用fgets

fgets(str[i], 40, stdin);

Problem 2 问题2

You are using incorrect size for word . 您使用的word大小不正确。 It needs to be of size 40 . 它的大小必须为40

char word[40];

Problem 3 问题3

You are returning a pointer to the first element of word from longestStrInArr . 您正在从longestStrInArr返回指向word的第一个元素的指针。 However, that memory is invalid once the function returns. 但是,一旦函数返回,该内存将无效。 Hence, your program has undefined behavior. 因此,您的程序具有未定义的行为。

You can fix it by returning the index of the longest string in str or by providing an input argument long enough to hold the longest string. 您可以通过返回str最长字符串的索引或提供足以容纳最长字符串的输入参数来修复它。

size_t longestStrInAr(char str[N][40], int size, int *length)
{
   size_t index = 0
   size_t i,j=0;
   int len = 0;
   *length = 0;

   for(i=0;i<size;i++){
      while(str[i][j]!='\0'){
         len++;
         j++;
      }
      if(*length<len){
         *length = len;
         index = i;
      }
   }
   return index;
}

and then use it as: 然后将其用作:

size_t index = longestStrInAr(str, size, &length);
printf("longest: %s \nlength: %d\n", str[index], length);

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

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