Skip to content

Acronim

Intro

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Task

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "acronym.h"
#include "string.h"
#include "ctype.h"
#include "stdio.h"
#include "stdlib.h"

char *abbreviate(const char *phrase)
{
    if(phrase==NULL) return NULL;
    size_t length= strlen(phrase);
    if(length==0)return NULL;

    char var[80]={0};
    strncpy(var,phrase,length);

    size_t i;
    for(i=0;i<length;i++)
    {
        if(var[i]== '-' || var[i]=='_')
        var[i]=' ';
    }

    char *token=strtok(var," ");

    static char *var02;
    var02=malloc(length);
    memset(var02,0,length);

    int ii=0;
    while (token)
    {
        //puts(token);
        var02[ii]=toupper(token[0]);
        ii++;
        token = strtok(NULL," ");
    }
    //puts(var02);
    return var02;
}
1
2
3
4
5
6
#ifndef ACRONYM_H
#define ACRONYM_H

char *abbreviate(const char *phrase);

#endif 

Last update: January 29, 2021

Comments