Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Largest_Element.cpp and Reverse_word.cpp in C++ #103

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions C++/Largest_Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;

int largest(int arr[], int n)
{
int i;

// Initialize maximum element
int max = arr[0];

// Traverse array elements
// from second and compare
// every element with current max
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];

return max;
}

// Driver Code
int main()
{
int arr[] = {10, 324, 45, 90, 9808};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Largest in given array is "
<< largest(arr, n);
return 0;
}
36 changes: 36 additions & 0 deletions C++/Reverse_words.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
using namespace std;

// Function to reverse words*/
void reverseWords(string& s)
{
// Reversing individual words as
// explained in the first step

int start = 0;
for (int end = 0; end < s.length(); end++) {

// If we see a space, we reverse the previous
// word (word between the indexes start and end-1
// i.e., s[start..end-1]
if (s[end] == ' ') {
reverse(s.begin() + start, s.begin() + end);
start = end + 1;
}
}

// Reverse the last word
reverse(s.begin() + start, s.end());

// Reverse the entire string
reverse(s.begin(), s.end());
}

// Driver Code
int main()
{
string s = "i like this program very much";
reverseWords(s);
cout << s;
return 0;
}