From f017b5df71f12435ca04add6e01e08ce0ba7c71a Mon Sep 17 00:00:00 2001 From: UG-SEP Date: Wed, 14 Apr 2021 20:19:59 +0530 Subject: [PATCH 1/4] added Count Words.c --- strings/c-or-cpp/count-words.c | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 strings/c-or-cpp/count-words.c diff --git a/strings/c-or-cpp/count-words.c b/strings/c-or-cpp/count-words.c new file mode 100644 index 00000000..748035ec --- /dev/null +++ b/strings/c-or-cpp/count-words.c @@ -0,0 +1,66 @@ +/* Program to find the total numbers of words in a sentence */ +#include +#include +#include + +//Function which calculate and return total numbers of words +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) + { +/*if sen[i]th index value is not equal to space and i is not the last index number because +the last index no. contain null and null is not alphabet so we not need to treat it as a alphabet*/ + if(sen[i]!=' '&&i!=strlen(sen)-1) + { + //if check is equal to 1(first time word occurrence after space) + if(check==1) + { + //increment word and assign 0 to check + word++; + check=0; + } + } + // if the sen[i]th index no. value is a space or null then run else + else + { + //assign 1 to check + check=1; + } + //increment i by 1 + i++; + } + return word; +} +//driver code +int main() +{ + //initialize required variables + char *sen,res; + // dynamically locating the address in sen variable + sen=(char*)malloc(sizeof(char)*1000); + printf("Enter a sentence: "); + //taking input + fgets(sen,1000,stdin); + // resizing the sen variable length so memory wastage can be prevent + sen=(char*)realloc(sen,strlen(sen)+1); + // calling count_words function which will return total words in res variable + res=count_words(sen); + //printing the total words + printf("Total number of words: %d",res); +} +/* +Test case 1: +Input: Enter a sentence: DSA is a Important Topic +Output: Total number of words: 5 + +Test case 2: +Input: Enter a sentence: Who are you? +Output: Total number of words: 3 + +Time Complexity O(n) where n is the length of string + +*/ From f662b859756b01e8bc54e872bcf4098644091137 Mon Sep 17 00:00:00 2001 From: Ujjwal <75884061+UG-SEP@users.noreply.github.com> Date: Wed, 14 Apr 2021 20:24:42 +0530 Subject: [PATCH 2/4] added count words program link --- strings/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/strings/README.md b/strings/README.md index 07bb97dd..cf6130bb 100644 --- a/strings/README.md +++ b/strings/README.md @@ -9,6 +9,7 @@ 5. [String Tokeniser](c-or-cpp/string-tokeniser.cpp) 6. [String Reversal](c-or-cpp/string-reverse.cpp) 7. [Permutation of String](c-or-cpp/Permutation-of-String.c) +8. [Count Words](c-or-cpp/count-words.c) ### C# 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 3/4] 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); From 1b2260be776474d3799743027e0761bf9acb60d0 Mon Sep 17 00:00:00 2001 From: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> Date: Thu, 15 Apr 2021 11:24:06 +0530 Subject: [PATCH 4/4] Fix typo in comments ./strings/c-or-cpp/count-words.c:47: returing ==> returning --- strings/c-or-cpp/count-words.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/strings/c-or-cpp/count-words.c b/strings/c-or-cpp/count-words.c index 82705d35..47437b7d 100644 --- a/strings/c-or-cpp/count-words.c +++ b/strings/c-or-cpp/count-words.c @@ -1,7 +1,7 @@ /* Program to find the total numbers of words in a sentence */ -#include -#include -#include +#include +#include +#include //Function which calculate and return total numbers of words int count_words(char *sen) @@ -33,6 +33,7 @@ the last index no. contain null and null is not alphabet so we not need to treat } return word; } + //driver code int main() { @@ -44,7 +45,7 @@ int main() if(sen==NULL) { fprintf(stderr, "malloc() failed to allocate memory\n"); - // returing 1 to tell that the program didn't run successfully + // returning 1 to tell that the program didn't run successfully return 1; } printf("Enter a sentence: "); @@ -57,6 +58,7 @@ int main() //printing the total words printf("Total number of words: %d",res); } + /* Test case 1: Input: Enter a sentence: DSA is a Important Topic