Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to see string length of a input. but can't find out. here is code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int i,vowel, consonent, digit, space, others, lenght;
i=vowel=consonent=digit=space=others=0;
string[] array1 = new string[25];
Console.WriteLine("Enter Your Sentence That U Want To Calculate.\nBut The Sentence Not More Than 25 Character:\n");
for (int j = 0; j < array1.Length; j++)
{
array1[j] = Console.ReadLine();
// Console.WriteLine("Enter");
}
lenght = array1.Length;
Console.WriteLine(lenght);
}
}
}
Posted
Comments
Karthik_Mahalingam 28-Nov-13 9:45am    
what error u r getting ??

I'm not sure, if I understood your question correctly. From your code, you're going to ask user to enter 1 sentence with length lesser then 25 characters. But you're asking for 25 sentences of arbitrarily length.

This code requests for user input, then input is limited to 25 characters. After it you can check, what types of characters is in input string.
C#
int maxLength = 25;
int digit = 0;
var theString = Console.ReadLine();
//If size is > 25 characters, limit to 25
theString = theString.Substring(0, theString.Length > maxLength ?
     maxLength : theString.Length);
//Iterate over all characters in string, checking, if it is digit, space or whatever.
foreach (var ch in theString)
{
    if (char.IsDigit(ch))
    {
        digit++;
    }
    //Similary for other types of char
}
Console.WriteLine(theString.Length);
 
Share this answer
 
v2
why not :
C#
string userInput = Console.ReadLine();
Console.WriteLine(String.Format("Your sentence is {0} characters long", userInput.Length));


Good luck!
Eduard
 
Share this answer
 
Comments
Durjoy39 28-Nov-13 9:47am    
Your answer is similar to my code. but i can't understand whats wrong with my code
Karthik_Mahalingam 28-Nov-13 10:01am    
try my solution u will get exactly 25 chars , after that it will come out of the loop.
Eduard Keilholz 28-Nov-13 9:57am    
No, your code puts the user input in an array, and you request the length of the array. I put it in a string and request the length of the string. That's a significant difference.
Hi Try this code...



C#
static void Main(string[] args)
        {
            int i, vowel, consonent, digit, space, others, lenght;
            i = vowel = consonent = digit = space = others = 0;
            char[] array1 = new char[25];
            Console.WriteLine("Enter Your Sentence That U Want To Calculate.\nBut The Sentence Not More Than 25 Character:\n");
            for (int j = 0; j < array1.Length; j++)
            {
                array1[j] = Console.ReadKey(false).KeyChar;

            }
            lenght = array1.Length;
            Console.WriteLine(lenght);
            Console.ReadKey(true);
        }
 
Share this answer
 
Comments
Durjoy39 28-Nov-13 9:57am    
oh god. this code also not working
Karthik_Mahalingam 28-Nov-13 10:04am    
its working fine in my machine, copy paste this code and try...
just check it by using
String.Length
this will get the length of the string and then check if it is less than 25
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900