Please see the code below. The user should enter a sentence and the code should return the acronyms of the sentence.
However, when I allocate *str and define N as 1.
"three letter acronym" >> should be >> "t"
In real time, "three letter acronym" >> is >> "tla"
This doesn't make sense. Please explain why this happens.
#include <stdio.h>
#include <stdlib.h>
#define N 1
#define M 35
char *acronyms (char *st, char *sentence);
int main()
{
char *str=(char*)(malloc(N*sizeof(char)));
char *sen=(char*)(malloc(M*sizeof(char)));
printf("enter... ");
gets(sen);
puts(acronyms(str, sen));
free(str);
free(sen);
return 0;
}
char *acronyms (char *st, char *sentence)
{
char *p = st;
char *q = sentence;
if (*q !=' ') {
*p =*q;
p++;
}
while (*(q+1)) {
if (*q==' ' && *(q+1)!= ' ') {
*p = *(q+1);
p++;
}
q++;
}
*p='\0';
return st;
}