How to check if the entered character is a digit? - c

Given task is:
Enter 10 characters. For each character entered the corresponding function prints whether it is a digit 0-9 or not.(Also I use older compiler if anyone concerns about my "gets()" and goal is to do this without pointers.)
So far I tried something like this, but for some reason it does not work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1];
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
}
for(i=0;i<10;i++){
if(strcmp(character[i],zero)>=0 && strcmp(character[i],nine)<=0){
printf("%d. character '%c' is a digit.", i, character[i]);
}
else{
printf("%d. character '%c' is not a digit.", i, character[i]);
}
putchar('\n');
}
return 0;
}
Also I tried this, but it can not output correctly characters:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1], pom[10][1];
int num_character[10];
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
strcpy(pom[i],character[i]);
num_character[i]=atoi(character[i]);
}
for(i=0;i<10;i++){
if(num_character[i]!=0){
printf("Character '%c' is digit.", pom[i]);
}
else{
printf("Character '%c' is not digit.", pom[i]);
}
putchar('\n');
}
return 0;
}
isdigit() also does not work after I include ctype header.

You are doing several things wrong here, gets is never recommended and fgets will put new line character in the end.
The syntax for strcmp is:
int strcmp(const char *s1, const char *s2);
You need two strings as input in strcmp but you are using a string and a character.
Here, it is better to use a character array than a 2D array.
#include <stdio.h>
int main(void)
{
char character[10]; //a simple character array
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
scanf(" %c", &character[i]); //scan characters
}
for(i=0;i<10;i++)
{
if(character[i] >= zero && character[i] <= nine) //print
printf("%d. %c is a digit \n", i+1, character[i]);
else
printf("%d. %c is not a digit \n", i+1, character[i]);
}
}

Related

Function is telling me the length of the string and not the number of occurrences of the char inputted by the user

#include <stdio.h>
#include <string.h>
int countLetters(char *string1, char letter){
int count=0;
for(int i=0; i<strlen(string1); i++){
if(string1[i]=letter){
count++;
}
}
return count;
}
int main(){
char string1[200];
char letter;
int count;
printf("\nEnter the string: \n");
fgets(string1, 200, stdin);
printf("\nEnter character to be searched: \n");
scanf("%c", &letter);
count = countLetters(string1, letter);
printf("\nThe number of occurrences: %d", count);
}
I was expecting for the function to output the number of times each letter of the array was equal to the char(letter) inputted by the user, but it is just giving me the length of the string.
Change the line:
if(string1[i]=letter){
to
if(string1[i]==letter){
Note, that the string1[i]=letter was overwriting data in string1[i].
You have to use equal equal to operator instead of assignment operator in if condition like this
if(string1==latter)
in your if condition if(string1=latter) value of latter variable is assign to string1[i]

Why does my program exit after taking an input?

Consider:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf("%[^\n]s", &name);
}
}
Whenever I am entering the value of n, it just prints (Enter n values) and exits the program. The for loop never runs. It ran successfully for the first time, but after that it just exits the program.
There were some answers that said it will not print anything. I don’t want it to print just to take input n times. It is not doing that.
My aim is to take n as input and then take strings of names (like harry, robin, etc.) n number of times as input.
Your code is a little incomplete. And there are a few errors here: scanf ("%[^\n]s", &name)
Do this and everything will be fine:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf(" %d", &n);
printf("Enter %d values\n", n);
for(int i=0; i<n; i++)
{
scanf(" %99[^\n]", name);
printf("%s\n", name);
}
return 0;
}
scanf is particularly unsuited for user input.
You probably want this:
int main() {
int n;
char name[100];
int number;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("Enter %d values\n", n);
for (int i = 0; i < n; i++)
{
// the space at the beginning of "%[^\n]"
// gets rid of the \n which stays in the input buffer
scanf(" %[^\n]", name); // also there sis no 's' at the end of the "%[^\n]" specifier
printf("name = %s\n", name); // for testing purposes
}
}
But this doesn't actually make much sense because the program is asking for n names, but at each run of the for loop the previous name will be overwritten with the new name.
Also be aware that scanf("%[^\n]", name); is problematic because if the user types more than 99 characters you'll get a buffer overflow.

how to count empty space in c

#include <stdio.h>
#define SIZE 30
int main()
{
char string1[SIZE];
printf("%s","Enter a string less than 29 characters");
scanf("%s",string1);
int countword=0;
int countvowel=0;
int countspace=0;
int numberofcons;
for(size_t i=0;i<SIZE;++i){
if(string1[i]==' '){
countspace++;
countword++;
printf("h");
}
if(string1[i]=='a'||string1[i]=='A'||string1[i]=='e'||string1[i]=='E'||string1[i]=='i'||string1[i]=='I'||string1[i]=='o'||string1[i]=='O'||string1[i]=='u'||string1[i]=='U'){
countvowel=countvowel+1;
printf("h");
}
}
numberofcons=SIZE-countvowel-countspace;
printf("Your sentence include\n");
printf("Number of words:");
printf("%d\n",countword);
printf("Number of spaces:");
printf("%d\n",countspace);
printf("Number of vowels:");
printf("%d\n",countvowel);
printf("Number of consonants and special characters:");
printf("%d\n",numberofcons);
printf("%s",string1);
}
so this is my program but somehow it does not run properly. the result shows every count i write is still 0 and I found the input string1 only reads til the first space how can I fix it

Scanning a char array after scanning an integer in C

I'm a newbie in programming. It is confusing me when I taking input of a char array after scanning an integer. It is not working properly.
The code is following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf("%[^\n]",a);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
Output is following:
The problem is due to the white spaces.After scanf("%d",&x); the last entered '\n' character is taken and saved the string a of scanf("%[^\n]",a).
To avoid this give space in scanf() statement
scanf(" %[^\n]",a);//give a space
Why to give a space?
By giving a space,the compiler consumes the '\n' character or any
other white space ('\0','\t' or ' ' ) from the previous scanf()
your code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf(" %[^\n]",a);//give a space
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
fflush(stdin);
scanf("%[^\n]",a);
fflush(stdin);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);
return 0;
}
This also works. The same goes here, /0 at the end adds up to the character scan and interferes. Using fflush(stdin) will discard any unnecessary input data including the /0.
Correct me if I am wrong as I too am a newbie at coding. :p
Replace scanf("%[^\n]",a); with scanf(" %99[^\n]", a);
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[30];
int x,y;
scanf("%d",&x);
scanf("%s",a); // get char array without inputing space
scanf(" %99[^\n]", a); // get char array, allowing inputing space
scanf("%d",&y);
printf("%d\n%s\n%d\n",x,a,y);
return 0;
}
Instead of %d use %d\n to consume the newline, so that the following command will not just read nothing:
scanf("%d\n",&x);
scanf("%[^\n]",a);
scanf("%d",&y);
printf("%d\n%s\n%d",x,a,y);

Lower to Upper Case Conversion with C

Problem Statement
I'm facing difficulty in solving a programming contest problem, which reads as follows:
You are given T name(s) in english letters. Each name will include some
of the uppercase letters from A to Z, some of the lowercase letters
from a to z and some spaces. You have to transform the name(s) from
lowercase to uppercase. Letters that are originally uppercase
will remain the same and the spaces will also remain in their
places.
Sample Input-Output
If I type this in...
5
Hasnain Heickal Jami
Mir Wasi Ahmed
Tarif Ezaz
Mahmud Ridwan
Md Mahbubul Hasan
the computer should output this...
Case 1: HASNAIN HEICKAL JAMI
Case 2: MIR WASI AHMED
Case 3: TARIF EZAZ
Case 4: MAHMUD RIDWAN
Case 5: MD MAHBUBUL HASAN
Note that exactly one space is required between the semi-colon and the initial letter of the name.
My Coding
This is what I've coded in C:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main(void)
{
int T, i;
char string [100];
scanf("%d", &T);
for (i=0; i<T; i++)
{
gets(string);
printf("Case %d: ", i);
while (string[i])
{
putchar (toupper(string[i]));
i++;
}
printf("\n");
}
getch();
return 0;
}
Now, this code fails to produce the desired output. Where am I doing it wrong? Is there any matter with my syntax? Can somebody guide me? Please bear in mind that I'm a middle-schooler and just a beginner in C.
You need to cycle over each letter of the string one-by-one.
In this code below, I have done that with variable K, which goes from 0 to the length of the string.
Variable I keeps track of the number of strings.
int main(void)
{
int T, i, k;
char string [100];
scanf("%d", &T);
for ( i = 0; i < T; ++i)
{
gets (string);
for(k=0; k<strlen(string); ++k)
{
putchar (toupper(string[k]));
}
}
getch();
return 0;
}
In response your question: IDEOne Link
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
int T, i,k;
char string [100];
scanf("%d ", &T);
for ( i = 0; i < T; ++i)
{
gets (string);
printf("[%d] : %s\n", i, string);
for(k=0; k<strlen(string); ++k)
{
putchar (toupper(string[k]));
}
putchar('\n');
}
return 0;
}
Please go through the code and implement the test cases scenarios as per your requirement.
#include <stdio.h>
#include<string.h>
int main(){
char string[100];
int i;
scanf("%s",string);
for(i=0;i<strlen(string);i++){
string[i]=string[i]-32;
}
printf("%s",string);
return 0;
}

Resources