LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2009, 02:54 PM   #1
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Rep: Reputation: 30
really can't understand why this wouldn't work


I'm doing this exercise from a book on C programming, where I have to insert one character string into another.

first of all here is what I have written:

Code:
#include <stdio.h>

void insertString(char source[], char string[], int startNum)
{
	int i = 0;
	int j = startNum;
	int k = 0;

	while ( string[i] != '\0')
		++i;

	while ( source[j - 1] != '\0')
	{	
		printf ("%s\n", source);
		string[i] = source[j];
		++i;
		++j; 	
	} 

	while ( string[k - 1] != '\0' )
	{
		source[startNum] = string[k];
		++startNum;
		++k;
	}
}

int main (void)
{
	int startNum;

	char source[] = "the wrong son";
	char string[] = "per";	

	void insertString (char source[], char string[], int startNum);

	printf ("Enter the position in the source string where the string is to be inserted: ");
	scanf ("%i", &startNum);

	insertString (source, string, startNum);

	printf ("%s\n", source);

	return 0;
}

simple program, it seems, but it just doesn't work how I would expect it to. can't figure out why. I enter number 10 when it asks for the number and it returns "on" instead of "the wrong person".

I inserted the "printf" statement into the second "while" loop, to see what the "source" prints out and it is the "source" that seems to change even though it is only used to assign its element values to "string" and not other way round.

it is really p*****g me off now!

my brain seems to be fried because it doesn't make any more sense to me.

please explain why my code doesn't work even though there probably is another way to do this!
 
Old 12-13-2009, 03:40 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by hubabuba View Post
I'm doing this exercise from a book on C programming, where I have to insert one character string into another.

first of all here is what I have written:

Code:
#include <stdio.h>

void insertString(char source[], char string[], int startNum)
{
	int i = 0;
	int j = startNum;
	int k = 0;

	while ( string[i] != '\0')
		++i;

	while ( source[j - 1] != '\0')
	{	
		printf ("%s\n", source);
		string[i] = source[j];
		++i;
		++j; 	
	} 

	while ( string[k - 1] != '\0' )
	{
		source[startNum] = string[k];
		++startNum;
		++k;
	}
}

int main (void)
{
	int startNum;

	char source[] = "the wrong son";
	char string[] = "per";	

	void insertString (char source[], char string[], int startNum);

	printf ("Enter the position in the source string where the string is to be inserted: ");
	scanf ("%i", &startNum);

	insertString (source, string, startNum);

	printf ("%s\n", source);

	return 0;
}

simple program, it seems, but it just doesn't work how I would expect it to. can't figure out why. I enter number 10 when it asks for the number and it returns "on" instead of "the wrong person".

I inserted the "printf" statement into the second "while" loop, to see what the "source" prints out and it is the "source" that seems to change even though it is only used to assign its element values to "string" and not other way round.

it is really p*****g me off now!

my brain seems to be fried because it doesn't make any more sense to me.

please explain why my code doesn't work even though there probably is another way to do this!
Debugging in general is comparing actual behavior witt expected one.

Code:
frptinf(stderr, "some kind of debug strings with formats if necessary\n", var1, var2, ... varN);
is your friend.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script does not work. Do not understand why. SteveInTallyFL Linux - Newbie 7 12-01-2008 09:32 AM
Does anyone understand exactly how to schedule work in kernel >= 2.6.20? sixerjman Linux - Kernel 1 09-03-2007 11:39 PM
Question about threads (I don't understand how they work) zahadumy Programming 10 12-13-2005 12:19 PM
i want others to understand my handy-work rblampain Programming 3 10-31-2004 01:15 AM
Things dont work when you dont understand withoutaclue Linux - Newbie 3 03-12-2003 09:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration