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 394 1
C++ program to Sort a Linked List Using Merge Sort 358 1
C++ Program to Implement a Linked List representation of a Binary tree 353 1
C++ Program to Check for balanced parentheses in an expression 250 1
C++ Program to Perform Inorder, Preorder, Postorder traversals in a binary tree. 289 1
C++ program to print Indian flag 385 1
C++ program to Convert a multi level linked list to a singly linked list 269 1
C++ program to print right view of a Binary tree using queue 243 1
C++ Program to implement Huffman Coding Compression Algorithm 1657 1
C++ Program to Create a Height Balanced Binary Tree 274 1
C++ program to implement Prims algorithm 635 1
C++ Program for BFS Traversal 288 1
C++ Progam to Evaluate a Prefix Expression 460 1
C++ Program to Implement Queue using Linked List 254 1
C++ implementation of Stack using Linked list 303 1
C++ program to find the intersection point of two linked lists 338 1
C++ program to count the inversions in the given array 278 1
C++ program to perform D.N.F sort 325 1
C++ program to print all possible subsets of a String 286 1
C++ program to count the number of ones in a binary representation of a number 310 1
C++ program to print all possible subsets of a set 322 1
C++ program to find the largest word in a String 287 1
C++ Program to print a matrix in Spiral order 365 1
C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal 312 1
C limits library 340 1
Program to add two Binary numbers 331 1

Comments