-3

plz help me to remove SIBABRT error for the following code,plz suggest me why this error occurs even after getting correct output

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

int main()
{
char x[25],y[25];
int i,j=0;
scanf("%s",x);
for(i=0;i<strlen(x);i++)
{
    if(x[i]>=97 && x[i]<=122)
    {
        y[j]=x[i]-32;
        j++;
    }
    else if(x[i]>=65 && x[i]<=90)
    {
        y[j]=x[i]+32;
        j++;
    }

}
printf("%s",y);}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    `scanf("%s",x);` --> `scanf("%24s",x);` – Sourav Ghosh Sep 05 '17 at 10:04
  • 1
    What is `97`? What is `122`? Why are you adding `32`? – Andrew Henle Sep 05 '17 at 10:06
  • What is your input? What is your expected output? – Jabberwocky Sep 05 '17 at 10:06
  • Have you actually tried debugging this? I'd expect you to have run your [mcve] within Valgrind or a similar checker, and to have investigated with a debugger such as GDB. Please [edit] your question to summarize what the debugging tools told you and why they didn't solve your problem. And read Eric Lippert's [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Toby Speight Sep 05 '17 at 10:08
  • You might be interested in `isupper()`, `islower()`, `toupper()` and `tolower()`. – Ilja Everilä Sep 05 '17 at 10:08
  • 1
    Don't say `97` when you can say `'a'`. The latter is portable and readable. The former is neither. – Petr Skocik Sep 05 '17 at 10:09
  • Hint: avoid "magic" numbers such as 97, 122, 65, 90, 32 etc. Use `'a'`, `'z'`, `'A'`, `'Z'` , `'a' - 'A'` etc. – Jabberwocky Sep 05 '17 at 10:09
  • As a side note, read this [when does process get SIGABRT](https://stackoverflow.com/questions/3413166/when-does-a-process-get-sigabrt-signal-6) – Ketan Mukadam Sep 05 '17 at 10:11

2 Answers2

1

Think of the difference between the source and the destination array, something is missing in the destination. The null-terminator.

Note: Both the arrays are local variable with automatic storage and unless initialized explicitly, their content is indeterminate.

Without a null-terminator in place, printf() will go out of bound for the supplied array while printing with %s which invokes undefined behavior.

The easiest way to handle this is zero-initilize the arrays, like

  char x[25] = {0} ,y[25] = {0};

which makes all the elements of the arrays set to 0, and the same value being used as null-terminator, you are not required to add one manually to the destination array.

Also, FWIW,

  • You should length-limit the input to prevent buffer overflow from longer than expected input, using something along the line scanf("%24s",x);

  • better to use fgets() to take the user input. If, iff, you have to use scanf(),you can use it but please use proper error checking.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

If the input is less than 25 characters, then the string will be null terminated. If the size exceeds the array size specified then it overwrites the memory not belonging to the array.

So fgets() is the alternative for scanf() in such case.

Nerdy
  • 1,016
  • 2
  • 11
  • 27