c - I can't remove the spaces from a string -


i'm doing this challenge on reddit.

now problem on bottom of program (unfinished), posted whole thing in case might have top of it.

now i'm trying remove spaces string user supposed enter, putting non-space characters character array. however, every time print try print string supposed removed spaces, prints first word. i, however, want print words 1 single combined word.

for example, when try convert hello world!, it's supposed give me helloworld!, it's giving me hello. i've tried different methods pointers , whatnot , every time face same problem. what's going on here?

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>  int main(void) {     char stringtobepacked[100]; //this string user puts in     char stringtobepackedwospaces[100]; //this gonna string without spaces      int , o , p; //incrementers/counters      double amtfchrsnstrng;     //ask user sentence input:     printf("please type in sentence can pack array:\n");     scanf("%s" , stringtobepacked);      //check amounts of letters in sentence minus spaces     (i = 0; stringtobepacked[o] > ; i++ , o++)     {         amtfchrsnstrng++;          if (stringtobepacked[o] == ' ')         {             amtfchrsnstrng--;         };     };     //now know amounts of letters in sentence minus spaces      //find root , make array size     double root = sqrt(amtfchrsnstrng);    //this find square root of (number) of letters in string     int rootup = ceil(root);  //this round double     char packingarray[rootup][rootup];    //have 2 dimensional array pack sentence      //make sure check wether sign space or not not pack these     (i = 0 , o = 0; stringtobepacked[i] != 0; i++)     {         if (stringtobepacked[i] != ' ')         {             stringtobepackedwospaces[o] = stringtobepacked[i];             o++;         }     }      stringtobepackedwospaces[o] = 0;     puts(stringtobepackedwospaces);      //loop through sentence in order pack array     // after end of column has been reached increment row     //now don't keep incrementing column increment backwards letters can packed upwards      //print array looping through ,  first columns rows      //the starting position of packing should randomised }; 

i suggest use fgets() instead of scanf().

scanf("%s" , stringtobepacked); 

instead of use

fgets(stringtobepacked,100,stdin); 

beacause scanf() %s specifer read input till encounters whitespace or upto field width if specified . gave desired output.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -