1.WAP TO COUNT THE NUMBER OF WORDS AND CHARECTERS IN A STRING. Python pls
Answers
Answer:
Python
sentence = "Beauty lies in the eyes of beholder";
wordCount = 0;
for i in range(0, len(sentence)-1):
#Counts all the spaces present in the string
#It doesn't include the first space as it won't be considered as a word
if(sentence[i] == ' ' and sentence[i+1].isalpha() and (i > 0)):
wordCount = wordCount + 1;
#To count the last word present in the string, increment wordCount by 1
wordCount = wordCount + 1;
#Displays the total number of words present in the given string
print("Total number of words in the given string: " + str(wordCount));
Output:
Total number of words in the given string: 7
C
#include <stdio.h>
#include <string.h>
int main()
{
char sentence[] = "Beauty lies in the eyes of beholder";
int wordCount = 0;
for(int i = 0; i < strlen(sentence)-1; i++) {
//Counts all the spaces present in the string
//It doesn't include the first space as it won't be considered as a word
if(sentence[i] == ' ' && isalpha(sentence[i+1]) && (i > 0)) {
wordCount++;
}
}
//To count the last word present in the string, increment wordCount by 1
wordCount++;
//Displays the total number of words present in the given string
printf("Total number of words in the given string: %d", wordCount);
return 0;
}
Output:
Total number of words in the given string: 7
JAVA
public class CountWords
{
public static void main(String[] args) {
String sentence = "Beauty lies in the eyes of beholder";
int wordCount = 0;
for(int i = 0; i < sentence.length()-1; i++) {
//Counts all the spaces present in the string
//It doesn't include the first space as it won't be considered as a word
if(sentence.charAt(i) == ' ' && Character.isLetter(sentence.charAt(i+1)) && (i > 0)) {
wordCount++;
}
}
//To count the last word present in the string, increment wordCount by 1
wordCount++;
//Displays the total number of words present in the given string
System.out.println("Total number of words in the given string: " + wordCount);
}
}
Output:
Total number of words in the given string: 7
C#
using System;
public class CountWords
{
public static void Main()
{
String sentence = "Beauty lies in the eyes of beholder";
int wordCount = 0;
for(int i = 0; i < sentence.Length-1; i++) {
//Counts all the spaces present in the string
//It doesn't include the first space as it won't be considered as a word
if(sentence[i] == ' ' && Char.IsLetter(sentence[i+1]) && (i > 0)) {
wordCount++;
}
}
//To count the last word present in the string, increment wordCount by 1
wordCount++;
//Displays the total number of words present in the given string
Console.WriteLine("Total number of words in the given string: " + wordCount);
}-
}
java
public class CountWords
{
public static void main(String[] args) {
String sentence = "Beauty lies in the eyes of beholder";
int wordCount = 0;
for(int i = 0; i < sentence.length()-1; i++) {
//Counts all the spaces present in the string
//It doesn't include the first space as it won't be considered as a word
if(sentence.charAt(i) == ' ' && Character.isLetter(sentence.charAt(i+1)) && (i > 0)) {
wordCount++;
}
}
//To count the last word present in the string, increment wordCount by 1
wordCount++;
//Displays the total number of words present in the given string
System.out.println("Total number of words in the given string: " + wordCount);
}
}