Capitalize the first and last character of each word in a string
Last Updated :
07 Aug, 2022
Given the string, the task is to capitalise the first and last character of each word in a string.
Examples:
Input: Geeks for geeks
Output: GeekS FoR GeekS
Input: Geeksforgeeks is best
Output: GeeksforgeekS IS BesT
Approach
- Create a character array of the String
- Run a loop from the first letter to the last letter.
- Check if the character is the starting or end of the word
- Check if the character is a small letter.
- If yes, then Capitalise the character of the string.
Below is the implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
string FirstAndLast(string str)
{
string ch = str;
for ( int i = 0; i < ch.length(); i++)
{
int k = i;
while (i < ch.length() && ch[i] != ' ' )
i++;
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;
}
int main()
{
string str = "Geeks for Geeks" ;
cout << str << "\n" ;
cout << FirstAndLast(str);
}
|
Java
class GFG {
static String FirstAndLast(String str)
{
char [] ch = str.toCharArray();
for ( int i = 0 ; i < ch.length; i++) {
int k = i;
while (i < ch.length && ch[i] != ' ' )
i++;
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 new String(ch);
}
public static void main(String args[])
{
String str = "Geeks for Geeks" ;
System.out.println(str);
System.out.println(FirstAndLast(str));
}
}
|
Python3
def FirstAndLast(string) :
ch = list (string);
i = 0 ;
while i < len (ch):
k = i;
while (i < len (ch) and ch[i] ! = ' ' ) :
i + = 1 ;
if ( ord (ch[k]) > = 97 and
ord (ch[k]) < = 122 ):
ch[k] = chr ( ord (ch[k]) - 32 );
else :
ch[k] = ch[k]
if ( ord (ch[i - 1 ]) > = 90 and
ord (ch[i - 1 ]) < = 122 ):
ch[i - 1 ] = chr ( ord (ch[i - 1 ]) - 32 );
else :
ch[i - 1 ] = ch[i - 1 ]
i + = 1
return "" . join(ch);
if __name__ = = "__main__" :
string = "Geeks for Geeks" ;
print (string);
print (FirstAndLast(string));
|
C#
using System;
class GFG
{
static String FirstAndLast(String str)
{
char [] ch = str.ToCharArray();
for ( int i = 0; i < ch.Length; i++)
{
int k = i;
while (i < ch.Length && ch[i] != ' ' )
i++;
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 new String(ch);
}
public static void Main(String []args)
{
String str = "Geeks for Geeks" ;
Console.WriteLine(str);
Console.WriteLine(FirstAndLast(str));
}
}
|
PHP
<?php
function FirstAndLast( $str )
{
$ch = $str ;
for ( $i = 0; $i < strlen ( $ch ); $i ++)
{
$k = $i ;
while ( $i < strlen ( $ch ) && $ch [ $i ] != ' ' )
$i ++;
$ch [ $k ] = chr (( $ch [ $k ] >= 'a' && $ch [ $k ] <= 'z' )
? (ord( $ch [ $k ]) - 32) : (ord( $ch [ $k ])));
$ch [ $i - 1] = chr (( $ch [ $i - 1] >= 'a' && $ch [ $i - 1] <= 'z' )
? (ord( $ch [ $i - 1]) - 32) : (ord( $ch [ $i - 1])));
}
return $ch ;
}
$str = "Geeks for Geeks" ;
echo $str , "\n" ;
echo FirstAndLast( $str );
?>
|
Javascript
<script>
function FirstAndLast(str)
{
var ch = str.split( '' );
for ( var i = 0; i < ch.length; i++)
{
var k = i;
while (i < ch.length && ch[i] != ' ' )
i++;
ch[k] = String.fromCharCode(ch[k] >= 'a' &&
ch[k] <= 'z' ? (ch[k].charCodeAt(0) - 32)
: ch[k].charCodeAt(0));
ch[i - 1] = String.fromCharCode(ch[i - 1] >= 'a'
&& ch[i - 1] <= 'z' ? (ch[i - 1].charCodeAt(0) - 32)
: ch[i - 1].charCodeAt(0));
}
return ch.join( '' );
}
var str = "Geeks for Geeks" ;
document.write( str + "<br>" );
document.write( FirstAndLast(str));
</script>
|
Output:
Geeks for Geeks
GeekS FoR GeekS
Time Complexity: O(N) where N is the length of the original string
Auxiliary Space: O(N) where N is the length of the original string
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...