0

my question is at the generate acronym function, how can i make this function work in a pointer arithmetic way instead of array subscripting. without messing up with the structures itself, the prof prhobited array subscribting so i have to do it with pointer arithmetic instead, anyone can land a hand?

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define B 2
#define N 8
typedef struct {

    int course_id;
    int course_quota;
    char course_name[50];
    char course_code[6];
    char course_acronym[N];
}course_t;

void generate_course_code(char *course_code, int course_id);
void generate_course_acronym(char *, char *);
void display();
course_t courses[B];
int main() {
    int i;
    for(i = 0; i < B; i++) {
        printf("Enter the course name: ");

        fgets(courses[i].course_name, sizeof(courses[i].course_name), stdin);
        generate_course_acronym(courses[i].course_name, courses[i].course_acronym);


        printf("Enter the course Quota: ");
        scanf("%d", &courses[i].course_quota);

        while ('\n' != getchar())
        {

        }
        courses[i].course_id = i;
        generate_course_code(courses[i].course_code, courses[i].course_id);


    }
    display();


    return 0;
}

 void generate_course_code(char *course_code, int course_id) {
    char str[6];
    course_id++;
    strcpy(course_code, "CSE");
    if (course_id < 10) {

        sprintf(str, "0%d", course_id);
    }
    else
        sprintf(str, "%d", course_id);
    strcat(course_code, str);

} 



void generate_course_acronym(char *course_name, char *course_acronym) {

    int j = 0;
    char *p = course_name;
    for (course_acronym[j++] = toupper(*p); *p != '\0'; p++) 
        if (*p == ' ') course_acronym[j++] = toupper(*(++p));
        course_acronym[j] = '\0';

} 


void display() {
    int x;

    for (x = 0; x < B; x++) {
        printf("%d. %s - %s  (%s) - %d \n", ++courses[x].course_id, courses[x].course_code, courses[x].course_name, courses[x].course_acronym, courses[x].course_quota);


    }

} 
4
  • 2
    Your professor is very kind as she has provided an example right in there; the code traverses course_name using pointer arithmetic (see, p++); Hint: create another pointer (say q) the way p is, point it to course_acronym and see what you come up with. Give it a try Commented Apr 28, 2018 at 16:19
  • No need to create additional pointers in the function, the arguments themselves (pointers) can be used as shown in post below. Commented Apr 28, 2018 at 17:18
  • 1
    @ryyker, you're right of course, but conceptually may be an easier jump. Commented May 2, 2018 at 12:22
  • 1
    @AhmedMasud - Thank you. Either way will work. The OP code illustrated one method. (as your comment pointed out.) My thought was to suggest an alternative approach, to make the point there is always more than one method to solve a problem. Commented May 2, 2018 at 12:26

1 Answer 1

1

Because the function arguments are provided as pointers, they can be used as is to achieve your goal using pointer arithmetic as shown below:
(explanations in comments).

void generate_course_acronym(char *course_name, char *course_acronym) 
{
    *course_acronym = (char)toupper(*course_name); //initialize acronym to capitalized form of first character
    while(*course_name)         //test for end of string (NULL)
    {
        if(*course_name == ' ') // detect spaces between strings
        {
            course_acronym++;   // advance acronym pointer
            course_name++;      // advance source pointer beyond space.
            *course_acronym = (char)toupper(*course_name); // assign next acronym character
        }
        course_name++;          // advance source to next address location
    }
    course_acronym++;           // advance pointer to one location beyond end of acronym
    *course_acronym = '\0';     // NULL terminate, making acronym a new string
} 

Note: The calling function must pass enough space in course_acronym to accommodate 1 byte for each word in course_name, + 1 extra byte. For example, if course_name is defined as:

char course_name[]={"This is a fine programming class"};

then course_acronym must be defined with space for at least 7 bytes. (count of words + 1 for NULL termination)

Sign up to request clarification or add additional context in comments.

1 Comment

Oops never mind of course it does hehe my test case was flawed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.