WEEK:8 PROGRAMMING ASSIGNMENT 1 SOLUTION (Jan 2020-Apr2020) An Introduction to Programming through C++ NPTEL 2020

  

               An Introduction To Programming Through C++


IIT Bombay.



Week 8- Programming Assignment 1:


Given below is a main program that reads a line from the keyboard and
prints the number of words in it.  For this it calls a function words() which really does the work.  You are to write the function words() and enter that into the text box.  The main program is already entered.
A "word" is a (contiguous) sequence of non space characters which is maximal, i.e. it cannot be extended on either side. So as an example, if the character string is "He came, he   saw, he conquered."  then the words are "He", "came,", "he", "saw,", "he", "conquered.".  Notice
that for simplicity we are treating punctuation marks also as part of words. Also, note that two words may be separated by several spaces, not necessarily just one. Finally, we may have spaces also at the beginning or the end of the character string.For your reference, the main() program calling the function words() is given below.

int main(){
  char line[80];
  cin.getline(line,80);
  cout << words(line) << endl;

Sample Test Cases
Input Output
Test Case 1
The quick brown fox, jumped , over,the lazy dog.
9
Test Case 2
I me myself
3
WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.

PROGRAM:

int words(char *s){
    
    int countWords = 0;
    int i = 0;
    char ch = s[i];
    char lastChar = ' ';
    while (ch != '\0'){
       
        if(ch == ' '){
      if(lastChar == ' '){
         ;
     }else{
  ++countWords;
     }

        }else if(countWords == 0){
         ++countWords;
        }
        lastChar = ch;
        ++i;
        ch = s[i];
    }
    return countWords;
}

WEEK-8 :PROGRAMMING ASSIGNMENT 2 AND 3 
CLICK ON BELOW LINK.
<<CLICK HERE FOR  WEEK 8:PROGRAMMING -02>> <<CLICK HERE FOR  WEEK 8: PROGRAMMING-03>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100%  correct solutions.

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages