Program Discussion :: Strings
54 / 60
Write a program to enter the string then count the length of the string.
Answer:
#include
using namespace std;
int main()
{
string str = "C++ Programming";
/* you can also use str.length()*/
cout
Asked In ::
Language:
Monika Jain
20 Jun, 2017 10:39 PM
<!DOCTYPE html>
<html>
<body>
<?php
function Strin_length() {
$str ="madam"; /* input string */
echo "Input string is :" . $str."<br/>";
$strlength=strlen($str);
echo "String length is: ".$strlength;
}
Strin_length();
?>
</body>
</html>
Language:
Lekha
7 Jul, 2017 9:30 AM
#include <stdio.h>
int main()
{
char s[1000], i;
printf("Enter a string: ");
scanf("%s", s);
for(i = 0; s[i] != '\0'; ++i);
printf("Length of string: %d", i);
return 0;
}
Language:
Naman
7 Jul, 2017 9:30 AM
#include <iostream>
using namespace std;
int main()
{
string str = "C++ Programming";
/* you can also use str.length()*/
cout << "String Length = " << str.size();
return 0;
}
Language:
Jaykumar
25 Aug, 2017 12:42 AM
import java.util.Scanner;
class WordCount
{
public static void main(String args[])
{
System.out.println("Enter the string");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int count = 1;
for (int i = 0; i < s.length() - 1; i++)
{
if ((s.charAt(i) == ' ') && (s.charAt(i + 1) != ' '))
{
count++;
}
}
System.out.println("Number of words in a string = " + count);
}
}
Language:
Madhu
25 Aug, 2017 12:42 AM
import java.io.*;
class Test
{
public static void main(String args[])
{
String Str1 = new String("Welcome");
System.out.print("String Length :"+ Str1.length());
}
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.util.Scanner;
public class StringLength
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string: ");
String str=sc.nextLine();
System.out.println("Length of the string is: "+str.length());
}
}