GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

help, problem with correctly getting input using scanf

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

//given the string WORDPROCESSING write a program to read the string from the terminal and output it as
//WORD PROCESSING

int main()
{
    //create 2 arrays for each part of separate word
    char part1[3];      //part1 with WORD has 4 elements but due to 0 indexing size of array is 3
    char part2[9];    //part2 with WORD has 10 elements but due to 0 indexing size of array is 9
    printf("input: ");
    
    scanf("%4c%10c",part1,part2);   //get 4 characters and store in part1, get 10 characters and store in part2
    printf("%s %s\n",part1,part2);  //printout

   ///please why do i get gord processingord as my output 
   
}

 

asked Dec 8, 2015 by aminu241

1 Answer

I get no compiler error or warnings with your code and output is as expected, its "WORD PROCESSING" 

answered Dec 8, 2015 by Swanky
my question is as to why  i get "gord processingord" as my output, the output i was aiming for was "word processing", ie splitting the conjoined word WORDPROCESSING into two parts

let me try running it on another ide, thanks

it doesnt give the correct output, the code is syntaxtically correct so the compiler isnt giving and error message. but if you run the code with the input WORDPROCESSING, it wont give you back the expected WORD PROCESSING but instead GORD PROCESSINGORD

Check this .... it gives correct output on my IDE

...