0

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. 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. 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

  • for one thing you are returning `word`, but it is allocated on the stack so it ceases to exist after the function returns. – AndersK Mar 09 '18 at 06:03
  • problem with returning pointer to local variable was already pointed out, but you should reset `len` and `j` before while loop. With every for loop iteration these variables are increased and it may lead to undefined behaviour - reading out of range of array. – rafix07 Mar 09 '18 at 06:20
  • See [Why `gets()` is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) for a discussion of why `gets()` is evil and shouldn't be used, even in classroom exercises, and even when the code is provided by your tutor. Well, you may be stuck with your tutor and their, but I worry about what other bad habits you're being taught. – Jonathan Leffler Mar 09 '18 at 15:26

2 Answers2

1

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.

Instead just keep a pointer to the longest string in the str array, don't copy anything and return that pointer. Alternatively, if you want to simplify it, return the i index of the longest string.

michalsrb
  • 4,703
  • 1
  • 18
  • 35
1

Problems I see:

Problem 1

Don't use gets. It's a security hole. Use fgets instead.

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

Problem 2

You are using incorrect size for word. It needs to be of size 40.

char word[40];

Problem 3

You are returning a pointer to the first element of word from longestStrInArr. 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.

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);
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • _However, that memory is **valid** once the function returns...._. I think you want to say - that memory is **invalid** once the function returns.. – H.S. Mar 09 '18 at 06:11