C++ program to find the largest word in a String














































C++ program to find the largest word in a String



Algorithm:
Here we take input  n as no.of characters in the string and then we use currLen and maxLen two variables to store current length and the maximum length of the word in the string .Firstly both the variables are intialized to zero. Currlen starts from the index-0 and moves upto the space in the string and this currentLen value is given to maxLen , after this the currentLen value is incrmented to the index of the  next word.The value of the currentLen and previous currentLen is compared in the for loop upto the null or '/o ' and the highest value is given to maxst variable and the word is given to maxst variable.
Program:
#include<iostream>
using namespace std;
int main()
{
// largest word in a sentence
int n;
cout<<"Enter the number of characters in the string"<<endl;
cout<<"Note:Also count the spaces or indentation as 1 character";
cin >>n;
cout<<"Enter the string : ";
cin.ignore();//function to ignore the spaces in the input string
char arr[n+1];
cin.getline (arr, n+1);//a standard library function that is used to read a string or a line from an input stream
cout<<"press enter";
cin.ignore();
int i=0;
int currLen= 0, maxLen = 0;
int st=0, maxst=0;
 while (1)
 {
  if(arr[i] == ' '|| arr[i] == '\0')
  {
    if(currLen >maxLen)
     {
     maxLen =currLen;
     maxst=st;
      }

    currLen = 0;
    st=i+1;
     }
else
  currLen++;
  if(arr [i] == '\0')
  break;
  i++;
}
cout <<"the length of the maximum word is "<< maxLen << endl;
for(int i=0; i<maxLen; i++){
cout <<  arr[i+maxst];
}
return 0;
}

Sample input 1:
Enter the number of characters in the string
Note:Also count the spaces or indentation as 1 character
50
Enter the string : an apple a day keeps the doctor away
press enter
Sample output 1:
the length of the maximum word is 6
doctor



More Articles of Shaik Aftab Ahmed:

Name Views Likes
C++ Program to Find the Frequency of Odd & Even Numbers in the given Matrix 311 1
C++ program to Sort a Linked List Using Merge Sort 283 1
C++ Program to Implement a Linked List representation of a Binary tree 268 1
C++ Program to Check for balanced parentheses in an expression 182 1
C++ Program to Perform Inorder, Preorder, Postorder traversals in a binary tree. 229 1
C++ program to print Indian flag 268 1
C++ program to Convert a multi level linked list to a singly linked list 182 1
C++ program to print right view of a Binary tree using queue 197 1
C++ Program to implement Huffman Coding Compression Algorithm 822 1
C++ Program to Create a Height Balanced Binary Tree 213 1
C++ program to implement Prims algorithm 310 1
C++ Program for BFS Traversal 217 1
C++ Progam to Evaluate a Prefix Expression 205 1
C++ Program to Implement Queue using Linked List 210 1
C++ implementation of Stack using Linked list 227 1
C++ program to find the intersection point of two linked lists 256 1
C++ program to count the inversions in the given array 236 1
C++ program to perform D.N.F sort 276 1
C++ program to print all possible subsets of a String 249 1
C++ program to count the number of ones in a binary representation of a number 256 1
C++ program to print all possible subsets of a set 280 1
C++ program to find the largest word in a String 242 1
C++ Program to print a matrix in Spiral order 278 1
C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal 266 1
C limits library 298 1
Program to add two Binary numbers 269 1

Comments