15

My code is like a text compressor, reading normal text and turns into numbers, every word has a number. It compiles in DevC++ but does not end, however, it does not compile in Ubuntu 13.10. I'm getting an error like in the title in Ubuntu "undefined reference to `strlwr'", my code is a little long so I am not able to post it here, but one of the error is from here:

//operatinal funcitons here


int main()
{

    int i = 0, select;

    char filename[50], textword[40], find[20], insert[20], delete[20];

    FILE *fp, *fp2, *fp3;

    printf("Enter the file name: ");

    fflush(stdout);

    scanf("%s", filename);

    fp = fopen(filename, "r");

    fp2 = fopen("text.txt", "w+");

    while (fp == NULL)
    {

        printf("Wrong file name, please enter file name again: ");

        fflush(stdout);

        scanf("%s", filename);

        fp = fopen(filename, "r");

    }

    while (!feof(fp))

    {

         while(fscanf(fp, "%s", textword) == 1)

        {

            strlwr(textword);

            //some other logic

        }

    }

.... //main continues
mrtgnccn
  • 173
  • 2
  • 2
  • 6
  • when calling any of the `scanf()` functions: 1) always check the returned value (not the parameter values) to assure the operation was successful. 2) when using the input specifiers `%s` and `%[...]`, always include a MAX CHARACTERS modifier that is one less than the length of the input field to avoid buffer overflow because a) they will continue to input characters until the terminating condition, so can overflow the input buffer (undefined behavior) and because those input format specifiers always append a NUL byte to the input. – user3629249 Jan 28 '18 at 20:42
  • 2
    regarding: `while (!feof(fp))` [why 'while( !feof(fp) )' is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user3629249 Jan 28 '18 at 20:47

2 Answers2

32

strlwr() is not standard C function. Probably it's provided by one implementation while the other compiler you use don't.

You can easily implement it yourself:

#include <string.h>
#include<ctype.h>

char *strlwr(char *str)
{
  unsigned char *p = (unsigned char *)str;

  while (*p) {
     *p = tolower((unsigned char)*p);
      p++;
  }

  return str;
}
P.P
  • 117,907
  • 20
  • 175
  • 238
  • 1
    Just in case `char` is signed, you should use `str[i] = tolower((unsigned char)str[i]);`. – Jonathan Leffler May 12 '14 at 20:48
  • @BlueMoon Thank you sir, I did it like you said and it became alive. However I saw strcmp() as a standart C function and that is why I used it. Thank you again. – mrtgnccn May 12 '14 at 21:43
  • strcmp() **is** a standard function, while strlwr() isn't. The documentation of strlwr() would have mentioned about it (I hope!). – P.P May 12 '14 at 21:46
  • @BlueMoon: FYI: on Linux (Ubuntu 14.04 specifically, with GCC 4.8.2 specifically, but I think it is valid for x86 and x86_64 versions of Linux generically), plain `char` is a signed type; the range is `CHAR_MIN == -128 && CHAR_MAX == +127`. Ditto for Mac OS X 10.9.2 Mavericks and GCC 4.9.0. – Jonathan Leffler May 13 '14 at 02:43
1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* strlwr(char* );
int main() 
{
  printf("Please Enter Size Of The String: \n");
  int a,b;
  scanf("%d",&a);
  char* str;
  str=(char*)malloc(sizeof(char)*a);
  scanf("\n%[^\n]",str);
  char* x;
  x=strlwr(str);
  for(b=0;x[b]!='\0';b++)
  {
    printf("%c",x[b]);
  }
  free(str);
  return 0;
}
char* strlwr(char* x)
{
  int b;
  for(b=0;x[b]!='\0';b++)
  {
    if(x[b]>='A'&&x[b]<='Z')
    {
      x[b]=x[b]-'A'+'a';
    }
  }
  return x;
}