Problem statement:- Program to Capitalize the first letter of each word in String.
Data requirement:-
Input Data:- str
Output Data:-str
Output Data:-str
Additional Data:- i
Program in C
Here is the source code of the C Program to Capitalize the first letter of each word in String.
Code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i=0;
for(i=0;i<strlen(str);i++)
{
if(i==0 || str[i-1]==' ')
{
str[i]=toupper(str[i]);
}
}
printf("After Converting String is :%s",str);
}
Input/Output:#include<string.h>
#include<ctype.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i=0;
for(i=0;i<strlen(str);i++)
{
if(i==0 || str[i-1]==' ')
{
str[i]=toupper(str[i]);
}
}
printf("After Converting String is :%s",str);
}
Enter your String:cs info com
After Converting String is:Cs Info Com
After Converting String is:Cs Info Com
Program in C++
Here is the source code of the C++ Program to Capitalize the first letter of each word in String.
Code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int i=0;
int len=0;
while(str[len]!='\0')
{
len++;
}
for(i=0;i<len;i++)
{
if(i==0 || str[i-1]==' ')
{
str[i]=toupper(str[i]);
}
}
cout<<"After Converting String is: "<<str;
}
#include<cstring>
using namespace std;
int main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int i=0;
int len=0;
while(str[len]!='\0')
{
len++;
}
for(i=0;i<len;i++)
{
if(i==0 || str[i-1]==' ')
{
str[i]=toupper(str[i]);
}
}
cout<<"After Converting String is: "<<str;
}
Input/Output:
Enter your String:cSinfo com
After Converting String is: CSinfo Com
After Converting String is: CSinfo Com
Program in Java
Here is the source code of the Java Program to Capitalize the first letter of each word in String.
Code:
Code:
import java.util.Scanner;
public class p9 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
for(int i=0;i<str.length;i++)
{
if(i==0 || str[i-1]==' ')
{
str[i]=Character.toUpperCase(str[i]);
}
}
System.out.print("After Converting String is: ");
for(int i=0;i<str.length;i++)
System.out.print(str[i]);
cs.close();
}
}
public class p9 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
for(int i=0;i<str.length;i++)
{
if(i==0 || str[i-1]==' ')
{
str[i]=Character.toUpperCase(str[i]);
}
}
System.out.print("After Converting String is: ");
for(int i=0;i<str.length;i++)
System.out.print(str[i]);
cs.close();
}
}
Input/Output:
Enter your String:
program java c python
After Converting String is: Program Java C Python
program java c python
After Converting String is: Program Java C Python
Program in Python
Here is the source code of the Python Program to Capitalize the first letter of each word in String.
Code:
Code:
str=input("Enter the String:")
j=0
newStr=""
for i in range(len(str)):
if i==0 or str[i-1]==' ':
ch=str[i].upper()
newStr+=ch
else:
newStr = newStr + str[i]
print("Your String is:", newStr)
j=0
newStr=""
for i in range(len(str)):
if i==0 or str[i-1]==' ':
ch=str[i].upper()
newStr+=ch
else:
newStr = newStr + str[i]
print("Your String is:", newStr)
Input/Output:
Enter the String:python java c plus plus
Your String is: Python Java C Plus Plus
Your String is: Python Java C Plus Plus
0 Comments
Please do not Enter any spam link in the comment box