I'm trying to get a number by using a getche() and then i have a functions that validates that number by getting the ascii code from it, at the same time if it gets validated it stores it in an char array, so i'm able to print it as a string, for some reason it is returning me junk. Any ideas what i'm doing wrong?
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include <string.h>
validate(int number , int size)//validates the using ASCII table and prints a string
{
char cast[size];
cast[size] = '\0'; //ads \0 to the end of the array
if((number >= 48) && (number <=57))
{
cast[size] = (char) number; //gets the number and casts it to a string
printf("\n%s", cast); //shows the number
}
}
void main(){
int numbers;
int size;
printf("\nHow many Number you want to type? : ");
scanf("%i", &size);
for(int i = 0; i < size ; i++)
{
printf("\nNumber : ");
numbers = getche();
validate(numbers, size);
}
}
Related
Hi how would you count the number of occurences in the given word like shown below because with the program I have right now it doesn't seem to room correctly.
#include <stdio.h>
int main(void)
{
char a;
char lang[] = "pneumonoultramicroscopicsilicovolcanoconiosis";
char i = 0;
char count = 0;
printf("pneumonoultramicroscopicsilicovolcanoconiosis\n");
printf("\nEnter the letter you want to find the number of\n");
scanf("%c", &lang);
for (i = 0; i <= 46; i++)
if (a == lang[i]) {
count++;
}
printf("Number of %c is %d..\n", a, count);
return 0;
Your scanf is the problem.
Try:
scanf("%c",&a);
declare count as int instead of char
also change scanf to take input a
#include <stdio.h>
int main(void)
{
char a;
char lang[] = "pneumonoultramicroscopicsilicovolcanoconiosis";
char i = 0;
int count = 0;
printf("pneumonoultramicroscopicsilicovolcanoconiosis\n");
printf("\nEnter the letter you want to find the number of\n");
scanf("%c", &a);
for (i = 0; i <= 46; i++)
if (a == lang[i]) {
count++;
}
printf("Number of %c is %d..\n", a, count);
return 0;
}
You probably shouldn't hard code the max length of the string (46) if at all possible in case you are given a longer string, but assuming it's an assigned assignment that is set it shouldn't be a problem.
i and count should also be ints if possible for a bigger size count. And &lang should be &a since lang is already assigned while a is your checker.
I am working on a program that, when specified by a number entered by the user, will only print out that number of characters. For example, if the user enters the number 10, then if 14 characters are entered (including newlines, blanks and tabs) only 10 characters will be printed. My code seems to work for the first three or so characters, then it prints out garbage. I'm not sure what is wrong.
#include <stdio.h>
#include <stdlib.h>
void findchars(char *abc, int number);
int main(void)
{
char *array; // the actual array
int num; // number of characters to read, becomes array value
printf("Number of characters:");
scanf_s("%d", &num);
array = (char *)malloc(num * sizeof(char));
findchars(array, num);
printf("The first %d characters: ", num);
puts(array);
free(array);
return 0;
}
void findchars(char *abc, int number)
{
int i;
printf("Type characters and I will stop at %d: ", number);
for (i = 0; i < number; i++)
{
abc[i] = getchar();
}
}
You are passing non-zero-terminated array to puts. If you want your program to work just create your array 1 item bigger and add '\0' in the end.
Edit: like this
array = (char *)malloc((num+1) * sizeof(char));
and then right before puts:
array[num] = '\0';
iI'm trying to understand how to properly use arrays in C and tried to write a simple program, which is supposed to take a 5-integers array and eliminate zeroes to the left. This is my attempt:
#include <stdio.h>
int main() {
int seq[5];
int i;
int cor[5];
int counter;
printf("Type the 5 numbers: ");
scanf("%s", &seq);
for (i=0; i<5; i++){
if (seq[i] != 0) {
for (counter=0; counter<5-i; counter++){
cor[counter]=seq[i+counter];
}
break;
}
}
printf("%s", cor);
return 0;
}
The idea was that when something such as 00101 was entered, the program would look at each entry and check whether it is 0. If it isn't, at the position i, it would write a new array in which the 0-th position is assigned the value of the original array at i, the 1-th position would be assigned the value at i+1 and so on, and then it would print this new array, which should have no useless zeroes to the left. But it just prints the original array. What is wrong? Sorry for the begginner's question.
You have two ways to do what you want.
using chars :
#include <stdio.h>
int main() {
char seq[6]; /* reserve one place for terminating null */
int i;
char cor[5];
int counter;
printf("Type the 5 digits : ");
scanf("%5s", seq); /* limit to 5 chars and seq is already an array : ne & */
for (i=0; i<5; i++){
/* should control seq[i] >= '0' && seq[i] <= '9' */
if (seq[i] != '0') {
for (counter=0; counter<6-i; counter++){ /* also copy terminating null */
cor[counter]=seq[i+counter];
}
break;
}
}
printf("%s", cor);
return 0;
}
but user could give you chars that are not digits.
using ints :
#include <stdio.h>
int main() {
int seq[5];
int i;
int cor[5];
int counter;
printf("Type the 5 numbers: ");
i = scanf("%d%d%d%d%d", seq, seq+1, seq+2, seq+3, seq+4); /* seq+i = &(seq[i]) */
/* should control i == 5 */
for (i=0; i<5; i++){
if (seq[i] != 0) {
for (counter=0; counter<5-i; counter++){
cor[counter]=seq[i+counter];
}
break;
}
}
printf("%d%d%d%d%d", cor[0], cor[1], cor[2], cor[3], cor[4]);
return 0;
}
You cannot store a string in an integer array. You have to store strings in character arrays.
I'm new in C programming language.
I need to get every digit separately that user have entered.
Here is my code:
#include <stdio.h>
int main()
{
int n[100];
printf("Enter a number: ");
scanf("%d",&n);
printf("%d %d %d",n[1],n[2],n[3]);
return 0;
} //i know that my code is not assigning like i want.
and now for example user entered a number like 123, i want the output like 1 2 3, How can i assign every digit to n[i] ? Without using string to int or int to string like atoi? Here is what Im going to do: User will enter a number and the program will search from Matrix 100x100 in row or column. i think i need to get the every digit separately to search.
No need to go to character array. The lats digit of a number n can be computed using n%10. Then you can remove the last digit using n /= 10. So this cycle would print the digits in reverse order:
void print_rev_digits(int n) {
while (n) {
printf("%d\n", n%10);
n /= 10;
}
}
And using a stack you can print the digits in the correct order. You can also use recursion for this(which will use stack for you). I am deliberately not posting a complete solution.
In this case you should read the user input character by character:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main()
{
char input[100];
int n[100];
printf("Enter a number: ");
if (fgets(input, sizeof(input), stdin)) { // attempt to read a line
int i;
for (i = 0; input[i]; i++) { // for each entered character
if (input[i] >= '0' && input[i] <= '9') { // is a digit
n[i] = input[i] - '0';
printf("%d ", input[i] - '0');
}
else if (isspace(input[i])) // end of entered integer
break;
else {
printf(stderr, "Input is not a number\n");
return -1;
}
}
printf("\n");
} else {
fprintf(stderr, "User did not enter valid input.\n");
}
return 0;
}
This program is supposed to convert the array of chars (string) into an array of ints by subtracting 97 from their ascii value (the input should be lower case cause a has an ascii value of 97). So if i enter the string abcd i should get 0123 but instead I somehow get this: 012134513789. I can't figure out where the problem is.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void userEnter(int*pattern, int n);
int main(void)
{
int n, i;
printf("What is the length of the array: ");
scanf("%d",&n);
int pattern[n];
printf("Enter the char array: ");
userEnter(pattern, n);
printf("The int array is: ");
for(i=0;i<n;i++)
{
printf("%d",pattern[i]);
}
printf("\n");
}
void userEnter(int*pattern, int n)
{
char input[n];
scanf("%s", input);
int i;
for(i = 0; i < n-1; i++)
{
pattern[i] = input[i]-97;
}
}
char input[n];
scanf("%s", &input);
should be
char input[n+1];
scanf("%s", input);
input is equivalent to &input[0]
You should also exit the for loop in userEnter when you encounter the nul character that ends the user-entered string. e.g. with something like
char* p = input;
while (*p != '\0') {
*pattern = (*p) - 'a';
p++;
pattern++;
}
As KingsIndian points out, you also need to increase the size of your input buffer. At present, you overflow that buffer and overwrite the loop counter i;
The length parameter n includes one character for null as well. So, if you input length for n 4 then you can only input 3 characters, for example abc because the 4th is for the null.
So you should change the declaration accordingly:
Change:
char input[n];
to:
char input[n+1];
Note that variable length arrays are allowed only since C99.