Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Simple loop in C#
Message
From
09/10/2006 13:05:53
Keith Payne
Technical Marketing Solutions
Florida, United States
 
 
To
09/10/2006 10:25:49
Irv Adams
MSC Managed Care, Inc.
Florida, United States
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01160532
Message ID:
01160623
Views:
16
>>>I have a simple programming assignment I'm trying to help my daughter with, but I'm a helpless FoxPro addict and I'm not sure how to do this in C.
>>>
>>>She needs to write a loop to parse a string backwards, so 'Hello World' becomes 'dlroW olleH' and so on. Very simple, I could do it in my sleep in Fox, but I'm awake and I don't know much about C.
>>>
>>>Thanks,
>>
>>In C or C#?
>>
>>C#:
>>
>>
>>			string source = "Hello World";
>>			string result = String.Empty;
>>			char[] srcArray = source.ToCharArray();
>>
>>			int i;
>>
>>			if (srcArray.Length > 0)
>>			{
>>				for (i=srcArray.Length-1;i>-1;i--)
>>				{
>>					result += srcArray[i];
>>				}
>>			}
>>
>>
>>Straight C is a little more code. You have to use the string library (and header).
>
>Keith,
>
>Just in case I don't receive a reply from her regarding C-version, can you outline the same procedure in straight C?
>
>Thanks,
>
>Irv.

I can get you close. I don't have access to a C compiler at work so there may be an error or two.
#include <string.h>

char *source = "Hello World";

int slen = strlen(*source);
char result[slen+1];

int i;

if (slen > 0)
for (i=0;i<=slen;i++)
result[i] = source[slen-i];
Previous
Reply
Map
View

Click here to load this message in the networking platform