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 239 1
C++ program to Sort a Linked List Using Merge Sort 226 1
C++ Program to Implement a Linked List representation of a Binary tree 221 1
C++ Program to Check for balanced parentheses in an expression 146 1
C++ Program to Perform Inorder, Preorder, Postorder traversals in a binary tree. 181 1
C++ program to print Indian flag 201 1
C++ program to Convert a multi level linked list to a singly linked list 152 1
C++ program to print right view of a Binary tree using queue 163 1
C++ Program to implement Huffman Coding Compression Algorithm 225 1
C++ Program to Create a Height Balanced Binary Tree 161 1
C++ program to implement Prims algorithm 235 1
C++ Program for BFS Traversal 174 1
C++ Progam to Evaluate a Prefix Expression 167 1
C++ Program to Implement Queue using Linked List 175 1
C++ implementation of Stack using Linked list 173 1
C++ program to find the intersection point of two linked lists 228 1
C++ program to count the inversions in the given array 215 1
C++ program to perform D.N.F sort 227 1
C++ program to print all possible subsets of a String 215 1
C++ program to count the number of ones in a binary representation of a number 229 1
C++ program to print all possible subsets of a set 246 1
C++ program to find the largest word in a String 221 1
C++ Program to print a matrix in Spiral order 229 1
C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal 227 1
C limits library 255 1
Program to add two Binary numbers 231 1

Comments