-1

Here is my code what I have tried,

I have to take a string from the user and as output i have to print the characters at even places and at odd places separated by a space

MY PROBLEM: In every test case i am assigning to both the strings at end a null character to conclude that my string has ended. Lets suppose i enter 2 test cases and now if enter a smaller string than first test case in result it is printing my character of 2nd test case but also printing the characters of test case because first string was larger than second

I want to ask how it can be possible if i have ended the string with a null character in every test case?

so why it is printing the value after the null character because that were the characters of previous test case because that string was larger and its character are still there in array but it should print till null character Please help me to achieve this.

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

int main() {
   int t,i,j,k;
   scanf("%d ",&t);
   while(t>0){
       char s[10000],s1[10000],s2[100000];
      scanf("%s",s);
       for( i=0,j=0,k=0;i<strlen(s);i++){
           if(i%2==0)
               s1[j++]=s[i];
           else
                s2[k++]=s[i];


       }
       s1[strlen(s1)]='\0';
       s2[strlen(s2)]='\0';
       printf("%s %s\n",s1,s2);
       t--;

   }
}
satyaGolladi
  • 180
  • 13
  • 4
    Read [*How To Debug Small Programs*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). StackOverflow is not a do-my-work service. Read also carefully the reference of every standard function you are using (e.g. of [strlen](https://en.cppreference.com/w/c/string/byte/strlen), of [scanf](https://en.cppreference.com/w/c/io/fscanf), etc...) – Basile Starynkevitch Aug 29 '18 at 13:14
  • 4
    This does not make sense: `s1[strlen(s1)]='\0';` If there was something different than `'\0'` at that position, `strlen` would have returned another value. You need to use index `j` and `k` to put the terminating 0 byte – Gerhardh Aug 29 '18 at 13:15
  • You must read this FAQ: [Getting a stack overflow exception when declaring a large array](https://stackoverflow.com/questions/571945/getting-a-stack-overflow-exception-when-declaring-a-large-array) – Lundin Aug 29 '18 at 13:29

1 Answers1

5
   s1[strlen(s1)]='\0';
   s2[strlen(s2)]='\0';

This line is erroneous. Replace it with

   s1[j]='\0';
   s2[k]='\0';

This is because strlen() only works with checking null in a null terminated string. And at this point your generated string is not yet null terminated.

Rizwan
  • 3,324
  • 3
  • 17
  • 38
  • last test case is not passing –  Aug 29 '18 at 13:25
  • it is saying that process terminated due to timeout –  Aug 29 '18 at 13:26
  • can anyone tell me the solution –  Aug 29 '18 at 13:26
  • What are your test strings ?plz share – Rizwan Aug 29 '18 at 13:27
  • https://hr-testcases-us-east-1.s3.amazonaws.com/18927/input09.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1535556477&Signature=Tp3ZBOxcfxs27DzY99sc55c%2FLkw%3D&response-content-type=text%2Fplain –  Aug 29 '18 at 13:28
  • this is my final test case in which i am failing it contains nearly 1 billion characters in a string –  Aug 29 '18 at 13:29
  • Ok, I will look in to this and will update you. For now make sure that char s[10000],s1[10000],s2[100000]; are big enough to hold the strings. As I see first 2 are 10000 and last one is 100000. – Rizwan Aug 29 '18 at 13:30
  • 1
    You want to store one billion characters in a char array? – Leon Trotsky Aug 29 '18 at 13:32
  • sorry in constraint length of string is max 10000 –  Aug 29 '18 at 13:33
  • Declaring arbitrarily huge arrays to avoid buffer overflows is bad practice. If you really don't know in advance how large a buffer needs to be, use malloc or realloc to dynamically allocate and extend the buffer as necessary, possibly using a smaller, fixed-sized buffer as an intermediary. – Leon Trotsky Aug 29 '18 at 13:34
  • but how can i use malloc if user is directly entering the string –  Aug 29 '18 at 13:40
  • if user will enter the number of character first then i can use malloc –  Aug 29 '18 at 13:41
  • or if i can use malloc in this case? then how –  Aug 29 '18 at 13:42
  • Please correct size of the arrays as suggested in last comment. – Rizwan Aug 29 '18 at 13:43
  • I got it thanx you rizwan sir –  Aug 29 '18 at 13:48