Structure programming practice

Thread Starter

anukalp

Joined Jul 28, 2018
158
Program : Write program to read and print alphabet Entered by the User
  1. It should be print only 26 English Letter A to Z
  2. It shouldn't print numeric value 1 to 0
  3. It shouldn't print symbol . @ # % & * ..etc
C:
#include <stdio.h>

int main(void)
{
    /* Decleare variable to store character */
    char alphabet;

    /* Type alphabet */
    printf ("Please type one alphabet : ");

    /*Read the  alphabet Entered by the User */
    scanf ("%c", &alphabet);

    /* Show The alphabet entered */
    printf ("The alphabet you entered is  : %c", alphabet);

    return 0;
}
Please type one alphabet : A
The alphabet you entered is : A

This program fail at two points
  1. If user type numeric value it show numeric value
  2. if user type any symbol it show symbol
How to read and print only one alphabet ?
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
Code:
  if (alphabet >= 'A' && alphabet <= 'Z')
    printf("The alphabet you entered is  : %c", alphabet);
Thanks dl324,

C:
#include <stdio.h>

int main(void)
{
    /* Decleare variable to store character */
    char alphabet;
  
    /* Type alphabet */
    printf("Please type one alphabet : ");
  
    /*Read the  alphabet Entered by the User */
    scanf ("%c", &alphabet);
  
    if (alphabet >= 'A' && alphabet <= 'Z')
        {   
          printf("The alphabet you entered is  : %c \n", alphabet);
        }
  
    else
        {
            printf("you haven't entered alphabet \n");
        }
      
    return 0;
}
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
How to check valid string
Valid : dennis
Invalid : dl324, dl@324
C:
//Read and print sring Entered by the User

#include <stdio.h>

int main (void)
{
    /* Decleare array to store 20 character */
    char name[20];
  
    /* Type Name */
    printf ("What's Your Name  \n");

    /*Read the  string Entered by the User */
    scanf("%s", name); 
 
    /* Show The Name entered by user */
    printf("you're name is: %s", name);
 
    return 0;
}
 
Last edited:

Thread Starter

anukalp

Joined Jul 28, 2018
158
What are your rules for a valid string? Upper/lower alphabetic characters only?
  1. It should be contain only with Letter A to Z Upper/lower alphabetic ( dennis)
  2. It shouldn't contain with numeric value 1 to 0 (dl324)
  3. It shouldn't contain with symbol . @ # % & * ..etc (dl@324)
 

dl324

Joined Mar 30, 2015
16,839
  1. It should be contain only with Letter A to Z Upper/lower alphabetic ( dennis)
  2. It shouldn't contain with numeric value 1 to 0 (dl324)
  3. It shouldn't contain with symbol . @ # % & * ..etc (dl@324)
Code:
main (void) {
  char name[20];
  char *cptr=name;

  printf ("Enter Your Name: ");
  scanf("%s", name);
  while (*cptr) {
    if (!isalpha(*cptr++)) {
      printf("Non-alpha character detected: %c.\n", *--cptr);
      exit(0);
    }
  }
  printf("you're name is: %s\n", name);
  return 0;
}
 
Last edited:

Thread Starter

anukalp

Joined Jul 28, 2018
158
I have tested your code original code gives error and warning so I have modified it

C:
#include<stdio.h>

int main(void)
{
  char name[20];
  char *cptr=name;
 
  printf ("What's Your Name: ");
  scanf ("%s", name);
 
  while (*cptr) {
      if ( !isalpha (*cptr++) ) {
          printf ("Non-alpha character detected: %c.\n", *--cptr);
          break;
    }
  }

  printf ("you're name is: %s\n", name);

  return 0;

}
warning: implicit declaration of function 'isalpha' [-Wimplicit-function-declaration]
if (!isalpha(*cptr++)) {

What's Your Name: dl123
Non-alpha character detected: 1.
you're name is: dl123

Program should be print only valid string but it's printing invalid string
 

dl324

Joined Mar 30, 2015
16,839
Program should be print only valid string but it's printing invalid string
You need to replace the break command with exit. Otherwise, you need to keep track of the number of invalid characters and use that to control printing if the string has invalid characters.

You need to include ctypes.h for the code I gave to compile (after I added the missing curly brace at the end).
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
You need to replace the break command with exit. Otherwise, you need to keep track of the number of invalid characters and use that to control printing if the string has invalid characters.

You need to include ctypes.h for the code I gave to compile (after I added the missing curly brace at the end).
Program works as I want
C:
#include<stdio.h>
#include <stdlib.h>
int main(void)
{
  char name[20];
  char *pointer=name;

  printf ("What's Your Name: ");
  scanf ("%s", name);

while (*pointer) {
    if (!isalpha(*pointer++)) {
      printf("Non-alpha character detected: %c.\n", *--pointer);
      exit(0);
    }

  }
  printf("you're name is: %s\n", name);
  return 0;

}
I have few points to understand in you're program

Why did you think you should use pointer in program ?

you have declared character pointer and initializing with array
char *pointer=name;

I think char *pointer=name; same as char *pointer=&name[0];

What's the meaning of this if (!isalpha(*pointer++))?
 

dl324

Joined Mar 30, 2015
16,839
Why did you think you should use pointer in program ?
It's a matter of style. I'm of the opinion that accessing pointers is more efficient, but others have disputed that. I use them anyway because I like how it looks.
you have declared character pointer and initializing with array
char *pointer=name;

I think char *pointer=name; same as char *pointer=&name[0];
They're equivalent. It's a matter of your preferred style. You could skip using a pointer and step through the array if you wanted, but I think using pointers gives cleaner looking code.
What's the meaning of this if (!isalpha(*pointer++))?
It evaluates isalpha() on the character *pointer is pointing to then increments the pointer to point to the next character.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
I have written a program to store record of student using structure.

Please take a look at this
C:
/*
Program to store record of student using structure
Author   : Anukalp
Language : C Programming
Date     : 23.8.2018
*/

/* Header files */
#include<stdio.h>
#include<string.h>

struct Student
{
    char Name[10];      /* Decleare variable to store 10 character */
    char Section;       /* Decleare variable to store 1character */
    int Roll_Number;    /* Decleare variable to store integer value */
    float parcentage;   /* Decleare variable to store float value */
} record;

/* Program start from here */
int main (void)
{
    strcpy(record.Name, "Mogali");  //Stores string
    record.Section = 'A';           //store character
    record.Roll_Number = 10;        //Store integer
    record.parcentage = 82.20;      //Store float number

     printf("Name        : %s \n", record.Name);         //Print Name of student on screen
     printf("Section     : %C \n", record.Section);      //Print Section of student on screen
     printf("Roll_Number : %d \n", record.Roll_Number);  //Print Roll Number of student on screen
     printf("parcentage  : %.2f \n", record.parcentage);   //Print parcentage of student on screen
 
     return 0; //Program complete successful
}
Name : Mogali
Section : A
Roll_Number : 10
parcentage : 82.20

Do you think program requires modification at any place or It can be written in the better way ?

How to do if I want to store records of 10 student's
 
Last edited:

Thread Starter

anukalp

Joined Jul 28, 2018
158
Is this homework?
No it's not homework. Main purpose to improve the programming skill's by writing program. So I wrote a program I thought there might be something wrong with program or it could be written in a better way

I posted it here for better advice
 

dl324

Joined Mar 30, 2015
16,839
I posted it here for better advice
Okay.

The first thing I'd do is not hardcode a 10 character limit for student name. I'd also be concerned about duplicate names, so I'd add last name and probably a unique student identifier, like ID number.

For 10 students, it's probably not worth worrying about wasted memory. If you made the array for 1000 students, I'd allocate an array of pointers and allocate storage for each one as it was needed. I'd do that for the name variable too; only allocate storage for what's needed.

Another factor is how you plan to access the data in the array. Will you only access in the order that it was entered? By student name or other student identifier?
 

WBahn

Joined Mar 31, 2012
29,976
You should only use global variables if you have a damn good reason (and damn good reasons DO exist -- however this isn't an example of one).

You have a pretty significant vulnerability because you use strcpy() to copy a name into your Name character array. If the name you are going to copy won't fit into the allocated space, then you will overwrite memory that doesn't belong to it, causing all kinds of unpredictable problems.

Unless you have a good reason, avoid using float type and use double. The float type only gives about 7 sig figs of precision making it very susceptible to errors in computations.

I'd recommend writing functions to work on a structure of this type and only let those functions directly access the structure's elements.
 

Thread Starter

anukalp

Joined Jul 28, 2018
158
You should only use global variables if you have a damn good reason (and damn good reasons DO exist -- however this isn't an example of one).

Unless you have a good reason, avoid using float type and use double. The float type only gives about 7 sig figs of precision making it very susceptible to errors in computations.

I'd recommend writing functions to work on a structure of this type and only let those functions directly access the structure's elements.
Can you give me some points for structure like you gave me in this thread post # 5 https://forum.allaboutcircuits.com/threads/c-programming-introduction-with-examples.150894/

If I get some points, work gets easier for me then I can try to achieve every points by writing program
 

dl324

Joined Mar 30, 2015
16,839
If I get some points, work gets easier for me then I can try to achieve every points by writing program
Change the Name member to be a pointer to char. Use malloc() to allocate memory sufficient for each name at runtime.

Give more information on how you plan to access the data for additional suggestions.
 
Top