.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Count no.of spaces between each word

Posted By: sram     Posted Date: January 23, 2015    Points:5   Category :ASP.Net
I want to check whether a string contains specified no. of spaces. For eg:- If the input string is AY 12Y 50W 123 1B, I want to count the spaces between each. If space between the words is not 3, an error should be generated. The code am using is shown below:-


int cnt = 0;
for (int k = 0; k < s.Length; k++)
{

if (s[k] == ' ')
{
do
cnt++;
while (Char.IsLetterOrDigit(s[k]) == true);
continue;

}
if (Char.IsLetterOrDigit(s[k]) == true && cnt == 3)
{
break;
}

else if (cnt > 3)
return cnt;

}
return cnt;

With this code I am able to count upto 12Y only. The iteration is not proceeding further as I gave break in between. But if break is not given all the input are treated as error. Can any one suggest a method to resolve the problem.

Responses

No response found. Be the first to respond this post
Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend