SolvedTwo C Language Problems

Posts 1–9 of 9 · Page 1 of 1
Two C Language Problems
Sup guys, I'm having problems on solving these two problems...

1. I think to solve this problem I have to use ASCII table..
Code:
Here is a small step forward. In this chapter you learned about integers and the type
 int. The 'C' can also represent uppercase letters, lowercase letters and a large
 variety of special symbols. The 'C' uses small integers internally to represent
 each different character. The character set that uses a computer and
 representations of whole numbers corresponding to those characters is called set
 character of the computer. You can print the whole number equal to the capitalized
 A, for example, executing the statement
 printf ("% d", 'A');
 Write a C program that prints the integer equivalents of some letters
 uppercase letters, lowercase letters and special symbols. At a minimum, determine the numbers
 equivalent to the entire set following: BCabc 0 $ 12 * + / and space character
 white.
2. This I have no ideias..
Code:
Write a program that receives input from a five-digit number, separate the number
 digits in their components and print them from one another by three spaces. For example, if the user enters 42339, the program must write

 4   2   3   3   9
If anyone could help, i'd aprecciate that ;]
Thanks
I assume you could get the input as a char array, check if each element of the array is a number and check for the length to see if it's 5 digits long, and then print each number with a space in between them. I only use C++, otherwise I'd try to post code, I know next to nothing about C's libraries. Maybe you should google the different ways to do what I mentioned in C?
I already know array, but the thing is that the question requests to do with what I learned with that chapter, which doesn't explain arrays..
Bumping thread, have you worked out a solution @gbuzz ?
Well i tried Problem 2 sortoff:

Code:
#include <stdio.h>
#include <stdlib.h>

#define String "42339"
main(){
      int i;
      char *cp;

      cp = String;
      while(*cp != 0){
              putchar(*cp);
	      printf("   ");
              cp++;
      }
      putchar('\n');
      exit(EXIT_SUCCESS);
}
You can in some cases use a pointer the same way as an array i believe?
Yes, you can use the subscript operator ([]) on pointers, so pointer[n] will access the n'th element of size pointer (in this case 1 character)

Code:
const char buf[32] = "Chicken shit";
char *ptr = buf;
ptr[7] = '-';
Now buf contains "Chicken-shit"


You shouldn't use a #define for a string. The pre-processor does a copy/paste of each #define to where it's referenced, so by the time your code gets compiled, it will look like this:

Code:
#include <stdio.h>
#include <stdlib.h>

main(){
      int i;
      char *cp;

      cp = "42339";
      while(*cp != 0){
              putchar(*cp);
	      printf("   ");
              cp++;
      }
      putchar('\n');
      exit(0);
}
Although it doesn't matter in this case, you have lost the pointer to your original string.
If that was some memory you had allocated yourself (i.e. not a variable on the stack) you would now have a memory leak.


The question says you'll have to take user input, I would use scanf
Quote Originally Posted by -Raz0r- View Post
Yes, you can use the subscript operator ([]) on pointers, so pointer[n] will access the n'th element of size pointer (in this case 1 character)



You shouldn't use a #define for a string. The pre-processor does a copy/paste of each #define to where it's referenced, so by the time your code gets compiled, it will look like this:

Code:
#include <stdio.h>
#include <stdlib.h>

main(){
      int i;
      char *cp;

      cp = "42339";
      while(*cp != 0){
              putchar(*cp);
	      printf("   ");
              cp++;
      }
      putchar('\n');
      exit(0);
}
Although it doesn't matter in this case, you have lost the pointer to your original string.
If that was some memory you had allocated yourself (i.e. not a variable on the stack) you would now have a memory leak.


The question says you'll have to take user input, I would use scanf
Could you please post this code with the use of an array, because i cant figure it out.
Code:
#include <stdio.h>
#include <stdlib.h>

main(){
      int i=0;
      char cp[6] = "42339";
      while(cp[i] != 0){
              putchar(cp[i]);
	      printf("   ");
              i++;
      }
      putchar('\n');
      exit(0);
}
Posts 1–9 of 9 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us