Solved

Function

Posted on 2011-03-04
31
333 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:HelpMeMaggi
31 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 35034342
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
 

Author Comment

by:HelpMeMaggi
ID: 35034394
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35034402
When you say point, are you refering to pointer (which is another way to do this). I was refering to an array index.
0
 

Author Comment

by:HelpMeMaggi
ID: 35034431
       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
 
LVL 32

Expert Comment

by:phoffric
ID: 35034448
So, after the for-loop completes, what is the value of i when the phrase is "how"?
0
 

Author Comment

by:HelpMeMaggi
ID: 35034458
It would be 0 right?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35034484
Add a printf statement to find out the value of i after the for-loop.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35034503
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
 

Author Comment

by:HelpMeMaggi
ID: 35034506
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35034520
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
 

Author Comment

by:HelpMeMaggi
ID: 35034584
Not sure what your asking but i is used to get every single character that in inputed by the user including spaces.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35034739
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
 

Author Comment

by:HelpMeMaggi
ID: 35034780
phrase[2]?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 32

Expert Comment

by:phoffric
ID: 35034812
>> 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
 
LVL 32

Expert Comment

by:phoffric
ID: 35034821
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35034959
Do you have any last minute questions before I go? I will be back later today.
0
 

Author Comment

by:HelpMeMaggi
ID: 35034961
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35034963
what is "mybackwards.h"  ? Could you also post your main function?
0
 

Author Comment

by:HelpMeMaggi
ID: 35034984
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35035010
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35035014
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35035022
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35035071
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35035078
If you want to continue, I'll see you later.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 35035230
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35035258
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
 

Expert Comment

by:thermoduric
ID: 35126309
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now