Count of quarter between dates - c#

I want to calculate the count of total quarters (of a year) in the given time span.
for example:
start date = 1-june -2009
end date = 18-july-2011
count should be = 10.
one more
start date = 4-Jan-2009
end date = 27-oct -2010
count =8.
I have not been able to get the correct result.

Your example is wrong: there are only 7 quarters between 4-Jan-2009 and 27-oct -2010
You could simply add a reference to the Microsoft.VisualBasic.dll to your project and use DateDiff:
VB:
Public Shared Function getQuartersBetween(ByVal d1 As Date, ByVal d2 As Date) As Int32
Return DateDiff(DateInterval.Quarter, d1, d2)
End Function
C#:
public static int getQuartersBetween(System.DateTime d1, System.DateTime d2)
{
return Microsoft.VisualBasic.DateAndTime.DateDiff(DateInterval.Quarter, d1, d2);
}
or you could write your own implementation:
public class Quarter
{
public static long GetQuarters(DateTime dt1, DateTime dt2)
{
double d1Quarter = GetQuarter(dt1.Month);
double d2Quarter = GetQuarter(dt2.Month);
double d1 = d2Quarter - d1Quarter;
double d2 = (4 * (dt2.Year - dt1.Year));
return Round(d1 + d2);
}
private static int GetQuarter(int nMonth)
{
if (nMonth <= 3)
return 1;
if (nMonth <= 6)
return 2;
if (nMonth <= 9)
return 3;
return 4;
}
private static long Round(double dVal)
{
if (dVal >= 0)
return (long)Math.Floor(dVal);
return (long)Math.Ceiling(dVal);
}
}
or in VB.NET:
Public Class Quarter
Public Shared Function GetQuarters(ByVal dt1 As DateTime, ByVal dt2 As DateTime) As Long
Dim d1Quarter As Double = GetQuarter(dt1.Month)
Dim d2Quarter As Double = GetQuarter(dt2.Month)
Dim d1 As Double = d2Quarter - d1Quarter
Dim d2 As Double = (4 * (dt2.Year - dt1.Year))
Return Round(d1 + d2)
End Function
Private Shared Function GetQuarter(ByVal nMonth As Integer) As Integer
If nMonth <= 3 Then
Return 1
End If
If nMonth <= 6 Then
Return 2
End If
If nMonth <= 9 Then
Return 3
End If
Return 4
End Function
Private Shared Function Round(ByVal dVal As Double) As Long
If dVal >= 0 Then
Return CLng(Math.Floor(dVal))
End If
Return CLng(Math.Ceiling(dVal))
End Function
End Class

Code for you : Try below code
public static void Main()
{
//Application.Run(new XmlTreeDisplay());
int monthdiuff = monthDifference(Convert.ToDateTime("01/04/09"), Convert.ToDateTime("10/27/10"));
Console.WriteLine(monthdiuff);
int totalQuater = (monthdiuff / 3) + (monthdiuff%3);
Console.WriteLine(totalQuater);
Console.ReadLine();
}
private static int monthDifference(DateTime startDate, DateTime endDate)
{
int monthsApart = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Math.Abs(monthsApart);
}

Without some code to look over I can't help you find your exact problem.
If it were me I would probably find the difference between the dates in days, then divide by number of days in a quarter (91 or so). I'm sure that C# has some kind of date parsing module that can read in the dates as a string, giving you two objects that you could then subtract to find the difference in days.

This is one crude form of calculating the quarters based on your assumptions, you can choose to modify as it is it works good enough
DateTime dt1 = new DateTime(2009, 1, 1);// new DateTime(2009, 6, 1);
DateTime dt2 = new DateTime(2010, 10, 27);// new DateTime(2011, 7, 18);
if (dt1.Month < 4)
dt1 = new DateTime(dt1.Year,1,1);
else if (dt1.Month < 7)
dt1 = new DateTime(dt1.Year,4,1);
else if (dt1.Month < 10)
dt1 = new DateTime(dt1.Year,7,1);
else
dt1 = new DateTime(dt1.Year,10,1);
if (dt2.Month < 4)
dt2 = new DateTime(dt2.Year, 3, DateTime.DaysInMonth(dt2.Year, 3));
else if (dt2.Month < 7)
dt2 = new DateTime(dt2.Year, 6, DateTime.DaysInMonth(dt2.Year, 6));
else if (dt2.Month < 10)
dt2 = new DateTime(dt2.Year, 9, DateTime.DaysInMonth(dt2.Year, 9));
else
dt2 = new DateTime(dt2.Year, 12, DateTime.DaysInMonth(dt2.Year, 12));
TimeSpan ts = dt2 - dt1;
int quarters = (int) ts.TotalDays/90;
Console.WriteLine(quarters);
I am baselining the dates to the start and end of the quarters as you want and then assuming for 90 day quarter transforming the diff as int. Works for your mentioned examples,see if it suits you well enough

If the definition of a quarter is a 90-day difference, it's easy of course:
internal static int GetNumberOfQuarters(DateTime p_DtStart, DateTime p_DtEnd)
{
TimeSpan span = p_DtEnd.Subtract(p_DtStart);
return (int)span.TotalDays % 90;
}
But that's not what you're looking for. What about this (not tested but you'll get the idea)
internal static class DateTimeTools
{
internal static int GetNumberOfQuartersBetweenDates(DateTime startDate, DateTime endDate)
{
int iYearStart, iYearEnd, iMonthStart, iMonthEnd, iDayStart, iDayEnd;
iYearStart = startDate.Year;
iYearEnd = endDate.Year;
iMonthStart = startDate.Month;
iMonthEnd = endDate.Month;
iDayStart = startDate.Day;
iDayEnd = endDate.Day;
int iYearDiff, iQuarterDiff, iDayDiff;
iYearDiff = iYearEnd - iYearStart;
iQuarterDiff = iMonthEnd % 3 - iMonthStart % 3;
iDayDiff = iDayEnd - iDayStart;
int iNumOfQuarters = 0;
// at least a year difference?
if ((iYearDiff > 0 && iQuarterDiff > 0) || iYearDiff > 0 && iQuarterDiff == 0 && iDayDiff >= 0)
{
iNumOfQuarters = iYearDiff * 4 + iQuarterDiff;
}
// at least a quarter difference?
// within different years
if ((iYearDiff > 0 && iQuarterDiff <= 0)) // eg, dec 2010 - feb 2011 iYearDiff 1 iQuarterDiff -3
{
if ((iQuarterDiff == -3 && iDayDiff >= 0) || iQuarterDiff > -3)
{
iNumOfQuarters = iQuarterDiff + 4;
}
}
// within the same year
if (iYearDiff == 0 && iQuarterDiff > 0)
{
if ((iQuarterDiff == 1 && iDayDiff >= 0) || iQuarterDiff > 1)
{
iNumOfQuarters = iQuarterDiff;
}
}
return iNumOfQuarters;
}
}
Regards,
Nico

public static string GetQuarter(this DateTime date)
{
var quarterList = new List<string>();
if (date.Month >= 1 && date.Month <= 3)
return "Q1";
else if (date.Month >= 4 && date.Month <= 6)
return "Q1,Q2";
else if (date.Month >= 7 && date.Month <= 9)
return "Q1,Q2,Q3";
else
return "Q1,Q2,Q3,Q4";
}
This too can be used as an extension method if you are expecting to get a list of Quarters, You can later on use GetQuarter().Split(new[] { ',' }).Count() to get the count.

Easy formula to get quarters difference:
{
int firstQuarter = getQuarter(first);
int secondQuarter = getQuarter(second);
return 1 + Math.Abs(firstQuarter - secondQuarter);
}
private static int getQuarter(DateTime date)
{
return (date.Year * 4) + ((date.Month - 1) / 3);
}

Related

Optimization Code

In my zeal to improve as developer, I want to know my code it is possible to improve, I am new in this and have very small logical
this is my code:
private int Period(DateTime date)
{
int period = 0;
if(date!= null)
{
int numeroMes = int.Parse(date.Month.ToString());
if(numeroMes <= 2)
{
period = 1;
}
else if (numeroMes <= 4 && numeroMes > 2)
{
periodo = 2;
}
else if (numeroMes <= 6 && numeroMes > 4)
{
period = 3;
}
else if (numeroMes <= 8 && numeroMes > 6)
{
period = 4;
}
else if (numeroMes <= 10 && numeroMes > 8)
{
period = 5;
}
else if (numeroMes <= 12 && numeroMes > 8)
{
period = 6;
}
}
return period ;
}
tkns for help me.
You can use the flooring of integer division to your favour which should be faster.
private int Period(DateTime date)
{
return (date.Month + 1) / 2;
}
However rounding might be easier to understand.
private int Period(DateTime date)
{
return (int)Math.Ceiling(date.Month / 2.0);
}
You can calculate the period since every period is just two months.
As DaveShaw pointed out, there is no need to check for the date being null because DateTime is a value type and thus cannot be null.
private int Period(DateTime date)
{
// already an int, no need to convert
var month = date.Month;
if (month % 2 == 0)
return month / 2;
else
return (month + 1) / 2
}
Here's another even shorter option that A.S. suggested.
private int Period(DateTime date)
{
return (int)Math.Ceiling(date.Month / 2.0);
// or...
// return (date.Month + 1) / 2;
// but I prefer the Ceiling option since it is more obvious what is happening
}
Alternatively, you could also reduce your code in this way...
private int Period(DateTime date)
{
int month = date.Month;
if (month <= 2)
return 1;
else if (month <= 4)
return 2;
else if (month <= 6)
return 3;
else if (month <= 8)
return 4;
else if (month <= 10)
return 5;
else
return 6;
}
Since I used return statements, there is no need for the second bool in each if statement.
As others said, this would better fit in https://codereview.stackexchange.com/, since it's not a question to a problem.
Now for some improvements to your code.
if(date!= null)
This will always be true, since DateTime is a value type (struct), which can't be null. Only Nullable<DateTime>/DateTime? can be null. So you can simply remove that.
int numeroMes = int.Parse(date.Month.ToString());
Here you are converting date.Month (which is an int) to a string and then parse it back to an int. Don't do that, date.Month is an int already:
int numeroMes = date.Month;
The rest is ok so far, but i guess in this line:
else if (numeroMes <= 12 && numeroMes > 8)
you really meant numeroMes > 10. Doesn't make a difference though as values 9 and 10 are matched before that.
Finally, you could shorten all those ifs to a simple formula:
return (int)Math.Ceiling(date.Month / 2.0);
Math.Ceiling rounds up the passed decimal number to the next integral number. So 1.5 would become 2 for example.

Get Date by passing dayofweek, weekofmonth, monthofyear and year using c#

I need a method which can return a date after taking 4 inputs, dayofweek, weekofmonth, monthofyear and year. I have tried the following but it fails when 4th week of month doesn't have all days, so I return 28 to be at safer side. I would like to have a complete solution and better than this if possible. Please ignore my parameters, I know I can improve it by passing a date instead. Here is my code;
public static DateTime GetDateByDayOfWeekOfMonthOfYear(int dayOfWeek, int weekOfMonth, int monthOfYear, int year)
{
var firstDayOfMonth = new DateTime(year, monthOfYear, 1);
var firstDay = (int)firstDayOfMonth.DayOfWeek;
var addor = 0;
if (firstDay == (int)DayOfWeek.Monday)
addor = 0;
if (firstDay == (int)DayOfWeek.Tuesday)
addor = 6;
if (firstDay == (int)DayOfWeek.Wednesday)
addor = 5;
if (firstDay == (int)DayOfWeek.Thursday)
addor = 4;
if (firstDay == (int)DayOfWeek.Friday)
addor = 3;
if (firstDay == (int)DayOfWeek.Saturday)
addor = 2;
if (firstDay == (int)DayOfWeek.Sunday)
addor = 1;
var resultantDate = firstDayOfMonth.AddDays((7 * weekOfMonth + addor) - (7 - dayOfWeek) - 1);
return resultantDate.Month == monthOfYear
? resultantDate
: firstDayOfMonth.AddDays(27);
}
You can do it this way - for the weekday of your choice, and for the first or a later occurrence of this weekday:
// Select year, month, weekday, and occurrence of weekday.
int year = 2015;
int month = 10;
DayOfWeek dayOfWeek = DayOfWeek.Monday;
int occurrence = 1; // Valid values: 1 to 5.
// Constants.
const int daysInWeek = 7;
const int maximumWeek = 5;
const int minimumWeek = 1;
occurrence = occurrence < minimumWeek ? minimumWeek : occurrence;
occurrence = occurrence > maximumWeek ? maximumWeek : occurrence;
DateTime first = new DateTime(year, month, 1);
int primoOffset = (dayOfWeek - first.DayOfWeek + daysInWeek) % daysInWeek;
DateTime dayInMonth = first.AddDays(primoOffset + daysInWeek * --occurrence);
if (dayInMonth.Month != month)
{
// Week 5 belongs to the next month.
// Return value for the last occurrence.
dayInMonth = dayInMonth.AddDays(-daysInWeek);
}
return dayInMonth;

What is the exact Excel Days360 algorithm?

I'm porting some calculations from Excel to C# which use the Days360 function (the default/US method). Using the Wikipedia page as a guide, I came up with this code:
public static int Days360(DateTime a, DateTime b)
{
var dayA = a.Day;
var dayB = b.Day;
if (IsLastDayOfFebruary(a) && IsLastDayOfFebruary(b))
dayB = 30;
if (dayA == 31 || IsLastDayOfFebruary(a))
dayA = 30;
if (dayA == 30 && dayB == 31)
dayB = 30;
return ((b.Year - a.Year) * 12 + b.Month - a.Month) * 30 + dayB - dayA;
}
private static bool IsLastDayOfFebruary(DateTime date)
{
if (date.Month != 2)
return false;
int lastDay = DateTime.DaysInMonth(date.Year, 2);
return date.Day == lastDay;
}
I tested it with a (small) range of inputs and the results mostly agree with Excel's native function except if I use 2015-02-28 for both a and b. My code returns 0 and Excel -2.
My result seems more reasonable but at this point, I'd prefer to calculate the exact same result as Excel. There might be other inputs where they disagree so I don't want to make a special case just for that date.
Does anyone know the exact algorithm that Excel uses?
EDIT: There was a glaring bug in the original code I posted which is unrelated to the question. I had already fixed that one but I copied from the wrong file when posting the question.
According to this Wikipedia article the Microsoft Excel Days360 function is equivalent to 30/360 BMA/PSA. So to get exact results as MS Excel we need to implement the BMA/PSA method. I have implemented the method.
private double Days360(DateTime StartDate, DateTime EndDate)
{
int StartDay = StartDate.Day;
int StartMonth = StartDate.Month;
int StartYear = StartDate.Year;
int EndDay = EndDate.Day;
int EndMonth = EndDate.Month;
int EndYear = EndDate.Year;
if (StartDay == 31 || IsLastDayOfFebruary(StartDate))
{
StartDay = 30;
}
if (StartDay == 30 && EndDay == 31)
{
EndDay = 30;
}
return ((EndYear - StartYear) * 360) + ((EndMonth - StartMonth) * 30) + (EndDay - StartDay);
}
private bool IsLastDayOfFebruary(DateTime date)
{
return date.Month == 2 && date.Day == DateTime.DaysInMonth(date.Year, date.Month);
}
I had the same need, I found the solution in the function on line 51 of this phpexcel library
dateDiff360
this is part of the class code for the calculation
/**
* Identify if a year is a leap year or not
*
* #param integer $year The year to test
* #return boolean TRUE if the year is a leap year, otherwise FALSE
*/
public static function isLeapYear($year)
{
return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
}
/**
* Return the number of days between two dates based on a 360 day calendar
*
* #param integer $startDay Day of month of the start date
* #param integer $startMonth Month of the start date
* #param integer $startYear Year of the start date
* #param integer $endDay Day of month of the start date
* #param integer $endMonth Month of the start date
* #param integer $endYear Year of the start date
* #param boolean $methodUS Whether to use the US method or the European method of calculation
* #return integer Number of days between the start date and the end date
*/
private static function dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, $methodUS)
{
if ($startDay == 31) {
--$startDay;
} elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !self::isLeapYear($startYear))))) {
$startDay = 30;
}
if ($endDay == 31) {
if ($methodUS && $startDay != 30) {
$endDay = 1;
if ($endMonth == 12) {
++$endYear;
$endMonth = 1;
} else {
++$endMonth;
}
} else {
$endDay = 30;
}
}
return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
}
This algorithm also include the optional parameter method:
int startMonthDays = 0;
int endMonthDays = 0;
double diff = 0;
if(method.Equals("TRUE"))
{
if(dtStartDate.getDay() < 30)
{
startMonthDays = (30 - dtStartDate.getDay());
}
else
{
startMonthDays = 0;
}
if(dtEndDate.getDay() < 30)
{
endMonthDays = dtEndDate.getDay();
}
else
{
endMonthDays = 30;
}
diff = (dtEndDate.getYear() - dtStartDate.getYear())*360 +
(dtEndDate.getMonth() - dtStartDate.getMonth() - 1)*30 +
startMonthDays + endMonthDays;
}
else
{
if(DateCalendar.daysInMonth(dtStartDate.getYear(), dtStartDate.getMonth()) == dtStartDate.getDay())
{
startMonthDays = 0;
}
else
{
startMonthDays = (30 - dtStartDate.getDay());
}
if(DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth()) == dtEndDate.getDay())
{
if(dtStartDate.getDay() < DateCalendar.daysInMonth(dtStartDate.getYear(), dtStartDate.getMonth()) - 1)
{
if(DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth()) > 30)
{
endMonthDays = DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth());
}
else
{
endMonthDays = dtEndDate.getDay();
}
}
else
{
if(DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth()) > 30)
{
endMonthDays = DateCalendar.daysInMonth(dtEndDate.getYear(), dtEndDate.getMonth()) - 1;
}
else
{
endMonthDays = dtEndDate.getDay();
}
}
}
else
{
endMonthDays = dtEndDate.getDay();
}
diff = (dtEndDate.getYear() - dtStartDate.getYear())*360 +
(dtEndDate.getMonth() - dtStartDate.getMonth() - 1)*30 +
startMonthDays + endMonthDays;
}
with
public static int daysInMonth (int year, int month)
{
if (DateTime.IsLeapYear(year) && month == 2)
{
return 29;
}
else
{
return table[month - 1];
}
}
and
private static readonly int[] table = new int[]{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Test the next
public static int Days360(DateTime a, DateTime b)
{
var dayA = a.Day;
var dayB = b.Day;
if (IsLastDayOfMonth(a) && IsLastDayOfMonth(b)) {
dayB = Math.min(30, dayB);
} else if (dayA == 30 && dayB ==31) {
DayB = 30;
}
if (IsLastDayOfMonth(a))
dayA = 30;
return ((b.Year - a.Year) * 360 + b.Month - a.Month) * 30 + dayB - dayA;
}
private static bool IsLastDayOfMonth(DateTime date)
{
int lastDay = DateTime.DaysInMonth(date.Year, date.Month);
return date.Day == lastDay;
}

converting a string to a date in C#

I was wondering how I would convert a string of four to six digits to a date in C#?
111110 would be 11 11 10
111 10 would be 11 1 10
1 1 10 would be 1 1 10
The pattern being mmddyy mmd yy m d yy
and the spaces are either ' ' or '\0' (the input I am given is not very clean)
This what I have so far and it works for all above cases, its just not very pretty:
Is there a more efficient solution to the above cases?
//Converts the given input string into a valid Date
private DateTime convertToDateFromString(string dateString)
{
int length = dateString.Length;
int month = 1;
int day = 1;
int year = 1;
bool gotMonth = false;
bool gotDay = false;
bool gotYear = false;
char c = ' ';
char peek = ' ';
string buffer = "";
DateTime bufferDate;
int count = 0;
try
{
//loop character by character through the string
for (int i = 0; i < dateString.Length; i++)
{
c = dateString[i];
if ((i + 1) < dateString.Length)
peek = dateString[i + 1];
else
peek = '\0';
if (c != ' ' && c != '\0')
{
buffer += c;
count++;
//Check if the month is done
if ((peek == ' ' || peek == '\0' || count == 2) && gotMonth == false)
{
count = 0;
gotMonth = true;
month = int.Parse(buffer);
buffer = null;
}
//Check if the day is done
else if ((peek == ' ' || peek == '\0' || count == 2) && gotDay == false && gotMonth == true)
{
count = 0;
gotDay = true;
day = int.Parse(buffer);
buffer = null;
}
//Check if the year is done
else if ((peek == ' ' || peek == '\0' || count == 2) && gotYear == false && gotMonth == true && gotDay == true)
{
count = 0;
gotYear = true;
year = int.Parse(buffer);
buffer = null;
if (year >= 80 && year <= 99)
year += 1900;
else if (year >= 0 && year <= 79)
year += 2000;
}
}
}
bufferDate = new DateTime(year, month, day);
}
catch (System.Exception ex)
{
bufferDate = new DateTime(1, 1, 1);
}
return bufferDate;
}
You discriminator here is the number of spaces. First get that:
string source = "....";
int spaceCount = source.Count(c => c == ' ');
Then create formatstrings for the expected range 0..2 . You can use the strings from the question except that you have to use M for the months:
var formats = new string[] { "MMddyy", "MMd yy", "M d yy" };
and then you can get your date :
DateTime r = DateTime.ParseExact(source, formats[spaceCount], null);
Add validations as required.
Try using
DateTime.TryParseExact(dateString, new string[] { "MMddyy", "MMd yy", "M d yy" }, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out result);
if you need to you can
dateString = dateString.Replace('\0', ' ');
before you begin
You should use one of the many Parse methods defined on DateTime.
These will take the string, an optional format string (describing the format of the datetime string) and an optional culture.
Take a look at Parse, ParseExact, TryParse and TryParseExact, some of which will take a string[] of the formats to try.
Additionally, here available string formats - standard and custom date and time format strings.

Parsing an RFC822-Datetime in .NETMF 4.0

I have an application written in .NETMF that requires that I be able to parse an RFC822-Datetime.
Normally, this would be easy, but NETMF does not have a DateTime.parse() method, nor does it have some sort of a pattern matching implementation, so I'm pretty much stuck.
Any ideas?
EDIT: "Intelligent" solutions are probably needed. Part of the reason this is difficult is that the datetime in question has a tendency to have extra spaces in it (but only sometimes). A simple substring solution might work one day, but fail the next when the datetime has an extra space somewhere between the parts. I do not have control over the datetime, it is from the NOAA.
Good ol' string manipulation:
Sun, 06 Jun 2010 20:07:44 +0000
1 2 3
0123456789012345678901234567890
string x = Sanitize(" Sun, 06 \t Jun 2010 \r\n 20:07:44 +0000 ");
int day = int.Parse(x.Substring(5, 2));
int month = Array.IndexOf(months, x.Substring(8, 3)) + 1;
int year = int.Parse(x.Substring(12, 4));
int hour = int.Parse(x.Substring(17, 2));
int minute = int.Parse(x.Substring(20, 2));
int second = int.Parse(x.Substring(23, 2));
int offsetSgn = (x[26] == "-") ? -1 : 1;
int offsetHour = int.Parse(x.Substring(27, 2));
int offsetMinute = int.Parse(x.Substring(29, 2));
DateTime result = new DateTime(year, month, day, hour, minute, second, 0);
TimeSpan offset = new TimeSpan(offsetHour, offsetMinute, 0);
// TODO: add offset...
with
string[] months = new string[12];
months[0] = "Jan";
months[1] = "Feb";
months[2] = "Mar";
months[3] = "Apr";
months[4] = "May";
months[5] = "Jun";
months[6] = "Jul";
months[7] = "Aug";
months[8] = "Sep";
months[9] = "Oct";
months[10] = "Nov";
months[11] = "Dec";
and
string Sanitize(string s)
{
if (s == null)
{
return null;
}
char[] buffer = new char[s.Length];
int pos = 0;
bool inSpace = true;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n')
{
if (!inSpace)
{
buffer[pos] = ' ';
pos++;
inSpace = true;
}
}
else
{
buffer[pos] = s[i];
pos++;
inSpace = false;
}
}
return new string(buffer, 0, pos);
}

Categories

Resources