From 38ef762be27fc8ac9730ac34cf4c90831949f311 Mon Sep 17 00:00:00 2001 From: Ujjwal <75884061+UG-SEP@users.noreply.github.com> Date: Thu, 15 Apr 2021 11:00:34 +0530 Subject: [PATCH] requested changes done --- strings/c-or-cpp/count-words.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/strings/c-or-cpp/count-words.c b/strings/c-or-cpp/count-words.c index 748035ec..82705d35 100644 --- a/strings/c-or-cpp/count-words.c +++ b/strings/c-or-cpp/count-words.c @@ -7,8 +7,6 @@ int count_words(char *sen) { int i=0,check=1,word=0; - //dynamically allocating memory - realloc(sen,strlen(sen)+1); //until i is less then length of sen variable the loop will run while(strlen(sen)>i) { @@ -42,6 +40,13 @@ int main() char *sen,res; // dynamically locating the address in sen variable sen=(char*)malloc(sizeof(char)*1000); + //checking whether the sen variable contain null + if(sen==NULL) + { + fprintf(stderr, "malloc() failed to allocate memory\n"); + // returing 1 to tell that the program didn't run successfully + return 1; + } printf("Enter a sentence: "); //taking input fgets(sen,1000,stdin);