I am trying to be able to read in 1 000 1 1 1 as like 100011. Also when I do printf of the string program crashes. Here is my code

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 32

int main(void){
    int i;
    int c;
    char a[MAX];
    char b[MAX];
    printf("input a: ");
    scanf("%c", &a);
    while (a[i]) {
          c = a[i];
          if (isspace(c)) c = a[i+1];
          i++;

          }
          printf("%s", c);
          getch();
          }

scanf("%c", &a); is not correct. Try using fgets() instead. After getting your number string, immediately print it out to make sure it's ok before moving on to process it. printf("%s", c); would not be correct either, you are trying to print out an int as a string.

line 12: that will only read one character, not the entire string. Call fgets() if you want to read the entire line, including spaces fgets(a, sizeof(a, stdin) ); line 13: Q: what is the value of variable i? A: Undefined because it was never initialized to anything.

line 19: %s wants a pointer to a string, but you are passing an integer. Pay more attention to the type of variables you pass to printf().

Isn't fgets only used from reading in from a file? Also

fgets(a, sizeof(a, stdin) );

gives error too few arguments

Also I realized maybe I should have in arrays instead of char arrays because I am working with bits (1's and 0's) so fgets shouldn't work

Isn't fgets only used from reading in from a file? Also

fgets(a, sizeof(a, stdin) );

gives error too few arguments

Also I realized maybe I should have in arrays instead of char arrays because I am working with bits (1's and 0's) so fgets shouldn't work

Too few for fgets and too many for sizeof..

fgets(a, sizeof(a), stdin );

I think he meant it as: fgets(a, sizeof(a), stdin ); (but just code your array size as the second parameter).

Yes, fgets can be used for file streams but in this case you are specifying stdin (generally keyboard unless set otherwise) as the stream.

A hint: you should take the data in to a char array and transfer it to an int array.

Do I use atoi to do that?

I think he meant it as: fgets(a, sizeof(a), stdin ); (but just code your array size as the second parameter).

Yes, fgets can be used for file streams but in this case you are specifying stdin (generally keyboard unless set otherwise) as the stream.

A hint: you should take the data in to a char array and transfer it to an int array.

You don't even have to go that far. Do it a char at a time. Hint: the ASCII code for '0' is 48 and the ASCII code for '9' is 57.

You don't even have to go that far. Do it a char at a time. Hint: the ASCII code for '0' is 48 and the ASCII code for '9' is 57.

I understand what you are saying but my programming skills is so bad. I dont see how I can cast a char array into an int array

You don't have to do any casting. Go element by element. If it's '1' (ASCII 49) how would you recover 1 from 49? How would you recover 2 from 50? Subtract 48, which is '0'.

char a = '4';
int x = a - '0'; // since chars are really ints

does this make sense

int main(void){
    int i = 0;
    int c;
    char a[MAX];
    char b[MAX];
    printf("input a: ");
    fgets(a, sizeof(a),stdin) ;
    while (a[i] != 32) {
          c = a[i] - '0';
          i++;
          }
          printf("%d", c);
          getch();
          

}

does this make sense

No. What happens when you enter

123
-----
123   
-----

It doesn't work. I also want it to store it in reverse so I did in this loop. Still doesnt work.

int main(void){
    int i = 0;
    int * c;
    char a[MAX];
    char b[MAX];
    printf("input a: ");
    fgets(a, sizeof(a),stdin) ;
    printf("%s\n", a);
    for (i = 31; i == 0; i --){ 
          c[i] = (a[i] - 48);
          
          }
          printf("%d", c);
          getch();

As far as I can tell, you have no idea what you want to accomplish.

Write down what you want to do in detail. Then break that description into steps.
Break the steps down as far as possible into minute instructions.

Now you have something you can translate into code.

And if you need help here, posting "It doesn't work" is not enough. If you're posting, it's obvious is doesn't work. Details. Enough details for someone that didn't write the code to understand what was supposed to happen and what did happen. Without this, we're guessing.

You don't have to do any casting. Go element by element. If it's '1' (ASCII 49) how would you recover 1 from 49? How would you recover 2 from 50? Subtract 48, which is '0'.

char a = '4';
int x = a - '0'; // since chars are really ints

I had meant that code snippet as more of a demonstration than something to actually incorporate directly. Remember I had said take into a char array and transfer to an int array.

WaltP speaks the truth, write down your objectives and then try to accomplish them on paper. Write out a number with spaces and "be the computer," read along and get the information you need and put it someplace else. It will save you time in the long run.

ok thanks for the advice i'll let you know how it goes.

i tink this is one solution to your problem: it takes the input code and sends it to another string without any spaces

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

int main()
{
  char initial[256];
  char * final = (char *) malloc(sizeof(final));
  char  temp;
  int i;
  
  printf("Enter string \n");
  gets(initial);
  printf("\n");
  strcpy(final, "");
  
  for( i = 0; i< strlen(initial); i++)
  {
       temp = initial[i];
          if (((int)temp) != 32) //32 is the ascii for a space
          {
          strcat(final, &temp);
          }
  
  }
  printf("%s\n", final);
  getch();
	
  return 0;
}

on line 13:

gets(initial);

Please don't encourage anyone to use gets. It has no restrictions to the size of input so it can clobber the adjacent memory causing a disaster. See this by WaltP.

i tink this is one solution to your problem: it takes the input code and sends it to another string without any spaces

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

int main()
{
  char initial[256];
  char * final = (char *) malloc(sizeof(final));
  char  temp;
  int i;
  
  printf("Enter string \n");
  gets(initial);
  printf("\n");
  strcpy(final, "");
  
  for( i = 0; i< strlen(initial); i++)
  {
       temp = initial[i];
          if (((int)temp) != 32) //32 is the ascii for a space
          {
          strcat(final, &temp);
          }
  
  }
  printf("%s\n", final);
  getch();
	
  return 0;
}

I think this code would be a great little test sample for "find the bugs". [I'm thinking of recent threads on the topic of code and job interview.] Maybe not all bugs, but "issues", "don't do this" things, etc.

I think this code would be a great little test sample for "find the bugs". [I'm thinking of recent threads on the topic of code and job interview.] Maybe not all bugs, but "issues", "don't do this" things, etc.

You are too kind with "Maybe not all bugs."
Without being pedantic here there are, the "issues" not "hidden or hard to find"

#include <stdio.h>
#include <string.h>
/* missing <stdlib.h> for malloc */

int main() /* missing the only two valid form of parameters: (void) or
              (int argc and char *argv[]) */
{
  char initial[256];
  char * final = (char *) malloc(sizeof(final)); /* casting malloc is not necessary if
                                                    stdlib is included */
  /* not checking that malloc returned a success to proper chunk of memory */
  /* sizeof(final) is only the sizeof of a char pointer. Hardly, enough for what you want
   * to do */


  char  temp; /* This should be declared before malloc if using C89 standards */
  int i;      /* See above comment */
  
  printf("Enter string \n");
  gets(initial); /* gets() is a trouble maker. You loose control of the quantity of
                    data an user can input, forget that exists. */
  printf("\n");
  strcpy(final, ""); /* empty string copied to final equals zero length in final string */
  
  for( i = 0; i< strlen(initial); i++) /* never loops, since final is zero */
  {
       temp = initial[i];
          if (((int)temp) != 32) //32 is the ascii for a space
          {
          strcat(final, &temp); /* &temp is a pointer to one char, the second parameter
                                   should be a string */
          }
  
  }
  printf("%s\n", final); /* it will display a newline only */
  getch(); /* getch() needs <cornio.h> It is not an standard function */

  /* final never gets free. Any memory allocated with malloc needs to be made free afterward */
	
  return 0; /* This is good */
}

I got it

// Read in input a
    printf("input a (seperate each bit with a space): ");
    fgets(x, sizeof(x),stdin);
    
    // Store the value removing whitespace and in reverse
         tok = strtok(x," ");    
   while( tok != NULL){
              a[idx] = atoi(tok);
              idx--;
              tok = strtok (NULL, " ");
              
              }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.