Computer Science, asked by sahiluday4215, 1 year ago

Write a program to capitalize the last word of a string

Answers

Answered by sahap2156
5

Answer:

Below is the implementation of the above approach.

// CPP program to capitalise the first

// and last character of each word in a string.

#include<bits/stdc++.h>

using namespace std;

string FirstAndLast(string str)

{

// Create an equivalent string

// of the given string

string ch = str;

for (int i = 0; i < ch.length(); i++)

{

// k stores index of first character

// and i is going to store index of last

// character.

int k = i;

while (i < ch.length() && ch[i] != ' ')

i++;

// Check if the character is a small letter

// If yes, then Capitalise

ch[k] = (char)(ch[k] >= 'a' && ch[k] <= 'z'

? ((int)ch[k] - 32)

: (int)ch[k]);

ch[i - 1] = (char)(ch[i - 1] >= 'a' && ch[i - 1] <= 'z'

? ((int)ch[i - 1] - 32)

: (int)ch[i - 1]);

}

return ch;

}

// Driver code

int main()

{

string str = "Geeks for Geeks";

cout << str << "\n";

cout << FirstAndLast(str);

}

// This code is contributed by

Answered by mn121
0

import java.util.*;

public class Main

{

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

String str,w;

int lsp;

System.out.print("Enter a string: ");

str = input.nextLine();

str = str.trim();

lsp = str.lastIndexOf(' '); //store index of last space

w = str.substring(lsp); // taking out last word

w = w.toUpperCase();

System.out.println("last word : "+w);

}

}

Hope it helps you...

Please mark it as brainliest...

☺️☺️☺️

Similar questions