Write a program to input a sentence and convert it into lowercase and print a new
sentence after interchanging first word with last word & vice-versa.
Example:
Sample input
WE ARE IN CYBER WORLD
Sample output
world are in cyber we
Answers
Answer:
// C++ program to convert
// first character uppercase
// in a sentence
#include<bits/stdc++.h>
using namespace std;
string convert(string str)
{
for (int i = 0;
i < str.length(); i++)
{
// If first character of a
// word is found
if (i == 0 && str[i] != ' ' ||
str[i] != ' ' && str[i - 1] == ' ')
{
// If it is in lower-case
if (str[i] >= 'a' && str[i] <= 'z')
{
// Convert into Upper-case
str[i] = (char)(str[i] - 'a' + 'A');
}
}
// If apart from first character
// Any one is in Upper-case
else if (str[i] >= 'A' &&
str[i] <= 'Z')
// Convert into Lower-Case
str[i] = (char)(str[i] + 'a' - 'A');
}
return str;
}
// Driver code
int main()
{
string str = "gEEks fOr GeeKs";
cout << (convert(str));
return 0;
}
def convert(s):
s_lower = s.lower()
s_arr = s_lower.split()
s_arr[0], s_arr[-1] = s_arr[-1], s_arr[0]
return " ".join(s_arr)
s = input("enter string: ")
print(convert(s))