473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

decrement a foreach


is there a way to decrement a foreach loop?

for example

string s = "cat";

foreach(char c in s)
//some how it goes backward here
Console.WriteLine(c);

and so the result would be

t
a
c

--
Texeme
http://texeme.com

Nov 16 '05 #1
10 10636
No. foreach internally uses IEnumerator which only has "MoveNext()" method

-vJ
"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:2S***************@newsread3.news.pas.earthlin k.net...

is there a way to decrement a foreach loop?

for example

string s = "cat";

foreach(char c in s)
//some how it goes backward here
Console.WriteLine(c);

and so the result would be

t
a
c

--
Texeme
http://texeme.com

Nov 16 '05 #2
Hi,

Why you think you go forwards?

You are sure you go forwards with
for i as integer = 0 to Cat.length-1
Cat(i)
next

Cor
Nov 16 '05 #3
Why not use a for loop?

string s = "cat";

for (int i = s.Length-1; i >= 0; i--)
Console.WriteLine(s.Substring(i,1));

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:2S***************@newsread3.news.pas.earthlin k.net...

is there a way to decrement a foreach loop?

for example

string s = "cat";

foreach(char c in s)
//some how it goes backward here
Console.WriteLine(c);

and so the result would be

t
a
c

--
Texeme
http://texeme.com

Nov 16 '05 #4
Sorry,

I thought I was answering from the general newsgroup otherwise I would have
done it in C#, however Mick did that already

Cor
Nov 16 '05 #5
Mick is right

"Mick Doherty" wrote:
Why not use a for loop?

string s = "cat";

for (int i = s.Length-1; i >= 0; i--)
Console.WriteLine(s.Substring(i,1));

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Elementary Penguin" <si*****@hare.krishna> wrote in message
news:2S***************@newsread3.news.pas.earthlin k.net...

is there a way to decrement a foreach loop?

for example

string s = "cat";

foreach(char c in s)
//some how it goes backward here
Console.WriteLine(c);

and so the result would be

t
a
c

--
Texeme
http://texeme.com


Nov 16 '05 #6
You could optionally resort the string in the reverse order and then run your
foreach loop as usual. eg.

class ReversedComparer:IComparer
{
public int Compare(object x,object y)
{
if((char)x < (char)y)
return 1;
else if((char)x > (char)y)
return -1;
else
return 0;
}
}
Array array = s.ToCharArray();
Array.Sort(array,new ReversedComparer());
//s = new String((char[])array);

foreach(char c in array)
{
Console.WriteLine(c);
}
"Elementary Penguin" wrote:

is there a way to decrement a foreach loop?

for example

string s = "cat";

foreach(char c in s)
//some how it goes backward here
Console.WriteLine(c);

and so the result would be

t
a
c

--
Texeme
http://texeme.com

Nov 16 '05 #7
Mick Doherty wrote:
Why not use a for loop?
I like having access to the iterator in the body of the for code.

So, being able to use char r, and having it be indexxed, is a real benefit.

I guess the solution is to pass in a reversed string, but that seems really
inefficient.

I guess this is why the c people invented pointers...


string s = "cat";

for (int i = s.Length-1; i >= 0; i--)
Console.WriteLine(s.Substring(i,1));


--
Texeme
http://texeme.com

Nov 16 '05 #8
couple of options:

1~ go backward:
for(int i=s.Length-1; i>=0; i--)
Console.WriteLine(s[i]);

2~ use a Stack:
Stack stack = new Stack();
foreach(char c in s)
stack.Push(c);
while(stack.Count>0)
Console.WriteLine((char)stack.Pop());

F.O.R.

Nov 16 '05 #9
Elementary Penguin <si*****@hare.krishna> wrote:
Mick Doherty wrote:
Why not use a for loop?
I like having access to the iterator in the body of the for code.

So, being able to use char r, and having it be indexxed, is a real benefit.


Just use the indexer of the string:

for (int i=s.Length-1; i >= 0; i--)
{
char r = s[i];
// ...
}

At that point you can do everything you could with a foreach, and more,
as you know the index (which you don't with foreach).
I guess the solution is to pass in a reversed string, but that seems really
inefficient.


If you really wanted to use foreach and you're only interested in
strings, you could always your own enumerator which iterates through a
string's characters backwards.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
Olorin,

2~ use a Stack:
Stack stack = new Stack();
foreach(char c in s)
stack.Push(c);
while(stack.Count>0)
Console.WriteLine((char)stack.Pop());

Little bit crazy to use, however nice as sample.

:-)

Cor
Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Elementary Penguin | last post by:
is there a way to decrement a foreach loop? for example string s = "cat"; foreach(char c in s) //some how it goes backward here Console.WriteLine(c);
9
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
5
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
8
by: Angel Tsankov | last post by:
Should pre/post increment/decrement return const or non-const? What about other functions?
5
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
6
by: Kevin Walzer | last post by:
This code: #include <stdio.h> int main(void) { int n1, n2; //two integers n1 = 1; n2 = 1;
3
by: subramanian100in | last post by:
Consider the code fragment: vector<intcontainer; container.insert(container.begin(), 10); int& ref = *--container.end(); From this, it looks like we can apply prefix decrement operator to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.