0

I want to copy the last four characters in one character array to another array. I tried doing it like I have shown below.

{
char date[20],day[2],year[4];
int n,i,j;
gets(date);
n=strlen(date);
j=n-1;
        for(i=3;i>=0;i--)
        {
            year[i]=date[j];
            j--;
        }
printf("%s",year);
}

but when I copied it, even though the second array is small, it copies the entire string and also the four characters. For example if the input was 16 july 1776 the output is year=177616 july 1776

What is the cause and solution to this?

3
  • Welcome to Stack Overflow. Please read the About page soon, and the page about how to create an MCVE (minimal reproducible example) sooner. Your code doesn't show declarations for i or j or n. It uses gets() — see Why gets() is too dangerous to be used. You've not allowed enough space for 4 digits for the year and a null terminator byte, nor have you added a null terminator byte. That's likely the cause of your trouble. Similarly with the day array, though you don't show that being set. Commented Jun 28, 2016 at 0:07
  • @JonathanLeffler declaring the year array as 'year[5]' solved the problem..thanks.but why wasn't the day array, eventhough it also is exactly the same size of the data needed to be stored(i want 2 characters stored and i have given the size as 2)having the same error. Commented Jun 28, 2016 at 0:34
  • You were lucky that it was not zeroed. The chances are there were a couple of control characters in the day array (or perhaps it was eliminated by the optimizer because it was unused), and the sequence of the arrays in ascending address order was year, day, date. When you didn't null terminate the strings, the printf continued reading until it finally came across the null byte inserted by gets(). So, it was good luck that your mistakes were easily spotted. But you were invoking undefined behaviour which is unconditionally a Bad Idea! Commented Jun 28, 2016 at 1:24

1 Answer 1

1

please be careful about somethings.first one as Jonathan said don`t use gets instead of that use scanf(). second one is you must always declare variables and assign a appropriate value to it at the declare time(my idea). and the last one, char arrays must have Null terminator or '\0' at the end of array to show this place is the end of your string. Always you should keep one place more for null for termination.

null sign = '\0'

for example for year you should declare like this:
char year[5];//4 digits for year + 1 for termination('\0')

here is your edited and working code.

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

int main()
{

char date[11],day[3],year[5];
scanf("%s",date);
int n=strlen(date);
int j=n-1;
year[4]='\0';
for(int i=3;i>=0;i--)
{
    year[i]=date[j];
    j--;
}
printf("%s",year);
getch();
}
Sign up to request clarification or add additional context in comments.

1 Comment

scanf("%s",date); is not an improvement over gets(). Could use scanf("%10s",date); or better fgets(data, sizeof data, stdin).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.