-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment0.c
More file actions
65 lines (62 loc) · 1.83 KB
/
Assignment0.c
File metadata and controls
65 lines (62 loc) · 1.83 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER 100000
//some prototypes
void generateHandle(char *);
void destroyArray(char**, int);
int main(){
char ** nameList;
int count;
//get how many names to process
scanf("%i", &count);
//allocate memory for name list
nameList = (char**)calloc(count, sizeof(char*));
//for loop for reading in names
for(int i = 0; i < count; i++){
//allocate for each name individually
nameList[i] = (char *) malloc(BUFFER);
//read individual name in
scanf(" %[^\n]s", nameList[i]);
}
//for each individaul name, generate handle
//handle print is in the function
for (int i = 0; i < count; i ++){
generateHandle(nameList[i]);
}
//detroy all dynamically allocated
//memory in this function
destroyArray(nameList, count);
return 0;
}
void generateHandle(char * name){
char * word;
//chop off each word and process
//them individually
word = strtok(name, " ");
//continue chopping untill the end of the string
while(word != NULL){
//if case for single letter name case
if(strlen(word) == 1){
//print single character
printf("%c", word[0]);
word = strtok(NULL, " ");
}
//else case for other multicharacter words
else{
//print first letter and last letter
printf("%c%c",word[0], word[strlen(word)-1]);
word = strtok(NULL, " ");
}
}
//newline to sperate generated handle
printf("\n");
}
void destroyArray(char ** nameList, int count){
//free each individual element of the array
for(int i = 0; i < count; i++){
free(nameList[i]);
}
//free the whole array
free(nameList);
}