• C

Function

How do you write a function that will read the string backwards. For example "he left her" would become "reh tfel eh" i do not want to use string.h for this but ctype.h and memory.h can be used but i'd rather not.
HelpMeMaggiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
Sure thing. You are required to do this with basics and no library functions.

The easiest way is to loop over the chars of the string until you find the terminating null byte. Then indexing from this position, you can traverse the string backwards until the index is 0 which is the first char of the string.

Give that a try.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HelpMeMaggiAuthor Commented:
So i have it to the point where it writes it to the null byte but i can't get it to reverse so its backwards.
0
phoffricCommented:
When you say point, are you refering to pointer (which is another way to do this). I was refering to an array index.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

HelpMeMaggiAuthor Commented:
       char phrase [100];
      int i;
            printf("Please enter a phrase ");
            gets(phrase);
            
            for (i = 0 ; phrase[i ] != 0; i++)
            {
                  printf("%c ", phrase[ i]);
            }


This is my code so far. im confused to what im supposed to do.
0
phoffricCommented:
So, after the for-loop completes, what is the value of i when the phrase is "how"?
0
HelpMeMaggiAuthor Commented:
It would be 0 right?
0
phoffricCommented:
Add a printf statement to find out the value of i after the for-loop.
0
phoffricCommented:
BTW - when posting code, it is usually a good idea to place in the Code box below (or you can highlight the code and hit the code button above). This gives line numbers that we might refer to later.

You can then hit the Preview button below to make sure that the formatting is good.
0
HelpMeMaggiAuthor Commented:
so i is equal to the amount of times the loop ran. so if its just how its 3 if its "how" its 5. correct?
0
phoffricCommented:
Correct. So, now that you have tried this on two different phrases, what is the meaning of i after the loop (in terms of the phrase you entered - not in terms of loops)?
0
HelpMeMaggiAuthor Commented:
Not sure what your asking but i is used to get every single character that in inputed by the user including spaces.
0
phoffricCommented:
So, i is the number of chars that you typed in.

If you know about writing functions, then you have basically written the body of the strlen function (without the printf, of course):
     http://www.cplusplus.com/reference/clibrary/cstring/strlen/

Then if you comment out the printf in the for-loop, then i is the length of the string.
If you type in how, then i = 3.

What index refers to the 'w' in phrase?
0
HelpMeMaggiAuthor Commented:
phrase[2]?
0
phoffricCommented:
>> phrase[2]?
Correct.

So, now you have a good idea of where to start to get the last char.
You can now try to write a for-loop (use k as an index setting it to the index of the last char) and make sure that the index remains valid as you decrement the index. This should enable you to print out the chars in reverse order. After the loop, add a '\n' for good measure.
0
phoffricCommented:
If using Windows, you can use the excellent VS 2010 Express debugger to help you figure out what is going on with your programs.

If you haven't already done so, you can download the free Visual Studio Expresss C++ 2010:
    http://www.microsoft.com/express/Downloads/

I like the VS C++ debugger so much that I wrote articles on it. To quickly get started (about 15 minutes learning curve), you can read:

   C/C++ Beginner's Debugging Guide

After becoming familiar with the basics, move onto these two articles:
   Breakpoint Tips for C/C++

   Watch, Memory, Stack Tips: C/C++

If using Linux, then I recommend the ddd graphical debugger - much better than gdb IMO. If you do not have it, you should be able to download it.
0
phoffricCommented:
Do you have any last minute questions before I go? I will be back later today.
0
HelpMeMaggiAuthor Commented:
Thank you i have figured it out.
I am now having an issue with getting the value of lenght out and using it in the function.
#include<stdio.h>
#include"mybackwards.h"


void backwards(char *lenght)
{
	
int i;
int k;

	for (i = 0 ; lenght[i] != 0; i++)
{
}
		
	for (k = i-1 ; lenght[k] != 0; j--)
	{
	printf("%c", lenght[k]);
	}
}

i can't seem to figure out how to get the value from char *lenght so the function will run.

Open in new window

0
phoffricCommented:
what is "mybackwards.h"  ? Could you also post your main function?
0
HelpMeMaggiAuthor Commented:
its just were i have the prototypes and my main is just
the usual<stdio.h>
#include
#include "mybackwards.h"

main ()
{
char back[] = "how";

backwards(back);
printf("%s", back);
return 0;
}
nothing complicated.  
0
phoffricCommented:
line 15: j is undefined

line 15: k = i-1 ;   // good

line 15: lenght[k] != 0

this test made sense since at the end of a string, there was a terminating null byte.

>>  make sure that the index remains valid as you decrement the index

But as you decrement k, you are correctly reading the phrase backwards. But what preceeds how? You are hoping that it is a zero? Actually, you are not permitted to use an index that is out-of-range of the array. You could actually crash your system.

At line 17, print the value of k and see if it is always valid for your array.
0
phoffricCommented:
Be sure to post compilable code, and preferably buildable code. If you have trouble, then you can ask for help in this problem. (In large project problems, the compile/build errors are usually separate questions.)
0
phoffricCommented:
Please feel free to ask a different question if you wish. However, since the question you asked is valid, there is no reason to delete this question.
0
phoffricCommented:
Well, we were almost done so why not finish up? Please feel free to ask a different question if you wish. However, since the question you asked is valid, there is no reason to delete this question.
0
phoffricCommented:
If you want to continue, I'll see you later.
0
sarabandeCommented:
refering to your last code posted:

1. don't call the char*  'length'. call it 'text' or 'phrase'.

2. if you pass a char* of unknown size, you better pass the size as well.

  void backwards(char * text, int sizetext)    
  {
      ...

Open in new window


with that you can check on maximum size and do not risk to run out-of-boundary.

   
 for (i = 0 ; i < sizetext && text[i] != 0; i++)

Open in new window


3.  the backwards loop must end when i is < 0

   
for (k = i-1 ; k >= 0; k--)

Open in new window


the check on terminating zero character only can made at the right side!!!

Sara
0
phoffricCommented:
You cannot pass the length of the string since as you pointed out in the OP, you do not want to use library functions such as strlen, which is fine for learning. So your function correctly computed the length of the string - this is nice and compact.
0
thermoduricModeratorCommented:
Since the asker has not returned to this question I am taking steps to close it by accepting the first comment by phoffric.

Although there was plenty of discussion that followed this comment it captures the essence of how the asker could resolve this problem and the rest of the discussion that followed served to refine this idea into a working solution (or, rather it would have ended up that way had the asker persisted).

- thermoduric -
EE Community Support Moderator
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.