C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 11th, 2012, 07:42 PM
Kai.R Kai.R is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Dublin, Ireland
Posts: 4 Kai.R User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 54 m
Reputation Power: 0
Send a message via Skype to Kai.R
Facebook
Function Question Help

Hi,

I have tried this question, but I dont get the output that I qant to get.

The first parts work fine, but printing converting an array to print out backwards it does not print out anything.

Quote:
The question is:

Question 4

Write a program containing the following functions
- A function to tell you the length of a string.
- function to remove the final new line character from a string.
- A function to copy a string from one character array to another.
- A function to copy a string from one character array to another in reverse order.
- [OPTIONAL] A function to compare two string returning 0 if the strings are the same 1 if string1 should come before string2 and -1 in the opposite case.

This program should not include the string.h library, and should contain a main function which tests the functions like this:

Sample input and output:
Please enter a string
aaaaaabbbbbb
The string has length 12
The original string is <aaaaaabbbbbb>
The copied string is <aaaaaabbbbbb>
The String and its copy are identical
The string reversed is <bbbbbbaaaaaa>
<aaaaaabbbbbb> comes before <bbbbbbaaaaaa>


And my attempt for the question is:

Code:
/* Program 128 - String Editor

Author:	Kai R
Date:	24/11/2012
*/

#include <stdio.h>
#include <stdlib.h>

#define MAX_STRING_SIZE 80

char String[MAX_STRING_SIZE];
char String_copy[MAX_STRING_SIZE];
char String_copy_reverse[MAX_STRING_SIZE];

void create_string(void);
void character_count(void);
void remove_newline_character(void);
void string_copy(void);
void string_copy_reverse(void);
int string_order(void);

int main(void)
{
	create_string();
	character_count();
	remove_newline_character();
	string_copy();
	string_copy_reverse();
	string_order();
	
	return 0;
}

void create_string(void)
{
	char character;
	int index=0;
	
	printf("\nPlease enter a string:\n\n");
	
	do
	{
		character = getchar();								//Reads charaters 
		
		String[index] = character;						//saves to array
		
		index++;
	}
	while(character != '\n');								//terminates loop on 'entre'
	
	String[index] = '\0';								//adds null charater
	
	printf("\nYour input was [%s]\n\n", String);			//Prints out data
	
	return;
}

void character_count(void)
{
	int index, character_count=0;
	
	for(index = 0; String[index] != '\0'; index++)
	{
		character_count ++;
	}
		
	printf("You entered %i characters [%s]", character_count, String);
	
	return;
}

void remove_newline_character(void)
{
	int index, character_count=0;
	
	for(index = 0; String[index] != '\0'; index++)
	{
		character_count ++;
	}
	
	String[character_count-1] = '\0';
	
	printf("\nThe final newline character has been removed\n");
	
	return;
}

void string_copy(void)
{
	int index;
	
	for(index=0; String[index] != '\0'; index++)
	{
		String_copy[index] = String[index];
	}
	
	printf("\nThe original string was [%s]", String);
	printf("\nThe copied string was [%s]\n\n", String_copy);
	
	return;
}

void string_copy_reverse(void)
{
	int index_1, index_2, index_3=0;

	for(index_1 = 0; String[index_1] != '\0'; index_1++)
	{
		index_2 ++;
	}
	
	for(index_1 = index_2; index_1 >= 0; index_1 --)
	{
		String_copy_reverse[index_3] = String[index_1];
		index_3++;
	}
	String_copy_reverse[index_3] = '\0';

	printf("\nThe string backwards is [%s]\n", String_copy_reverse);
}

int string_order(void)
{
	int index, same=0, string1_before_string2, string2_before_string1;
	
	for(index=0; String[index] != '\0'; index++)
	{
		if(String[index] == String_copy_reverse[index])
		{
			same=1;
			string1_before_string2=0;
			string2_before_string1=0;
		}
		else if(String[index] < String_copy_reverse[index])
		{
			same=0;
			string1_before_string2=1;
			string2_before_string1=0;
		}
		else if(String[index] > String_copy_reverse[index])
		{
			same=0;
			string1_before_string2=0;
			string2_before_string1=1;
		}
	}
	
	if(same==1)
	{
		printf("\nThe strings are bothe equal\n");
		printf("%s is the same as %s", String, String_copy_reverse);
		
		return 0;
	}
	else if(string1_before_string2==1)
	{
		printf("\nThe first string goes before the second\n");
		printf("%s goes before %s", String, String_copy_reverse);
		
		return 1;
	}
	else if(string2_before_string1==1)
	{
		printf("\nThe second string goes before the first\n");
		printf("%s goes before %s", String_copy_reverse, String);
		
		return -1;
	}
}


This is the output I get when I run it:

Code:
C:\Computer Programing Programs\Program125_to129>Program128_string_editor.exe

Please enter a string:

test

Your input was [test
]

You entered 5 characters [test
]
The final newline character has been removed

The original string was [test]
The copied string was [test]


The string backwards is []

The second string goes before the first
 goes before test
C:\Computer Programing Programs\Program125_to129>

Reply With Quote
  #2  
Old December 12th, 2012, 06:01 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
1) Avoid using global variables. When your programs get larger, you'll be happy without them

2) Once you get rid of global variables, probably the string_copy function will also need to write a '\0' to the destination string. In your current code, the '\0' is already there due to global variables being initialized.

3) your immediate problem -- for the string "test" the 1st for loop will set index_2 to 4. The first character copied in the 2nd loop, then, is String[4] which is the '\0'. Rethink this function, taking into account the '\0' terminator.

4) I didn't read any further

Reply With Quote
  #3  
Old December 15th, 2012, 10:56 AM
admiraln admiraln is offline
Still Learning
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Montreal, Canada
Posts: 55 admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level)admiraln User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 18 h 57 m 51 sec
Reputation Power: 38
Learning to debug

What you really need at this time is to learn how to debug a program. When the results don't match to the expectation of what you think the code should do you have to see line by line, variable change by variable change what is happening.

Your ultimate debugging goal is to use a debugger and single step through program. That you should learn on a program that works properly first and then break the program in one spot and see how you can see the break. REPEAT on several different programs to gain skill.

This would be a separate learning exercise from solving the current problem. I do not recommend learning the debugger and debugging a program at the same time. You will not know if the results you get are from mistakes in your use of the debugger or mistakes in code.

My suggestion for right now is the very basic "insert print statements" technique. Place them with unique labels throughout your program. Concentrate on one section at a time. Display the variables you want to understand and try find the reason why they are set to the values you see.

Concentrate on one section at a time.

BDB suggested not to use global variables. This is strongly recommended BUT do not redo your code right now. The act of using that technique will add complications and possible errors to this code. Same learning problem like the debugger.

Debug this as it is and once you have learned to handle the string and loops rewrite it with passing parameters to functions.
This code is disposable: 10 or 20 assignments in your future it
will nor longer be relevant to you because you will know the techniques automatically.

Reply With Quote
  #4  
Old December 15th, 2012, 07:12 PM
Kai.R Kai.R is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Location: Dublin, Ireland
Posts: 4 Kai.R User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 54 m
Reputation Power: 0
Send a message via Skype to Kai.R
Facebook
Thank you for the help,
After looking at one of my function I found my mistake,
I never initilised index_2, and I forgot to consider the null character when reversing the string.


here is my correction:^^
Code:
void string_copy_reverse(void)
{
	int index_1, index_2=0 /*correction*/, index_3=0;

	for(index_1 = 0; String[index_1] != '\0'; index_1++)
	{
		index_2 ++;
	}
	
	 index_2-- /*correction*/;
	
	for(index_1 = index_2; index_1 >= 0; index_1 --)
	{
		String_copy_reverse[index_3] = String[index_1];
		index_3++;
	}
	String_copy_reverse[index_3] = '\0';

	printf("\nThe string backwards is [%s]\n", String_copy_reverse);
}


Now as to rewriting the code without using global varibles I have one question:

How do I return an array, I did some research and people said to use a pointer to the first element, but all of my tries didn't really work. What is the easiest way to return a array from a function?

Thank you fro your help.

Reply With Quote
  #5  
Old December 15th, 2012, 08:46 PM
BobS0327 BobS0327 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 118 BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level)BobS0327 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 18 h 48 m 29 sec
Reputation Power: 44
Check this link for solving your problem.

Or you could do a pass by reference function as indicated below:

Code:
#include <stdio.h>
#include <string.h>

void getdata(char* input, int inputsize)
{
	// use inputsize to be sure we don't go out of bounds
	strcpy(input, "We have updated the variable");
}

int main(void)
{
	char data[30] = {0};
	strcpy(data, "Testing 123");
	printf("BEFORE data is %s\n", data);
	getdata(data,sizeof data);
	printf("AFTER data is %s\n", data);
	return 0;
}

Last edited by BobS0327 : December 15th, 2012 at 09:54 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Function Question Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap