I am posting this for a suggestion.
I want to know which is the optimized way for writing a C code for the below problem.

question is to ouptut the below string
"Post New Thread "
to
"Thread New Post"

I have 2 methods.
1. Read the each character in the input string and store it in stack(using recursion) and form a string during exiting the function (till a space is found) and print it in reverse.
2. Read each string and push to stack( using strstr() function, after getting the pointer to a 'space' in the string , store each word to stack(may be using recursion) and then print while exiting the recursive function)

Which one is more optimized? In terms of performance, suppose i have a big stack area... Or is there any other better method.( I dont need the code, i need the logic only...)

I am basically confused in a way that , is it better to use functions like strstr() when we write programs in the performance point of view. I dont know the internal implementation of these string functions..( Are these functions non standard? Some times i never see these functions inside string.h when googling..)

Recommended Answers

All 10 Replies

strstr() is not a function that would be useful for this purpose. I would use strtok() to split the string into individual words, put each word in an array of char pointers, then print the array backwards from highest to lowest array indices.

<deleted -- duplicate post>

strstr() is not a function that would be useful for this purpose. I would use strtok() to split the string into individual words, put each word in an array of char pointers, then print the array backwards from highest to lowest array indices.

Thanks.. but how abt the performance? between storing it into array and pushing to stack ? which is the best one..

and are these functions standard??

C programs do not have the ability to push stuff on the stack like assembly language does with the push and pop instructions. So if you have something else in mind you need to explain it to me.

>>and are these functions standard??
Yes. all the functions in string.h are standard.

C programs do not have the ability to push stuff on the stack like assembly language does with the push and pop instructions. So if you have something else in mind you need to explain it to me.

>>and are these functions standard??
Yes. all the functions in string.h are standard.

As i did in my first post, i meant by using recursive function calls.... when we call a function recursively , it will automatically push the local variables to stack right?
in my case , to print in reverse order, i thought recursion would be suffiecient

As i did in my first post, i meant by using recursive function calls.... when we call a function recursively , it will automatically push the local variables to stack right?
in my case , to print in reverse order, i thought recursion would be suffiecient

You mentioned optimized in your first posting, then calling a function recursively is probably not the way because of the overhead generated by each function call..

You mentioned optimized in your first posting, then calling a function recursively is probably not the way because of the overhead generated by each function call..

The overhead will be because of the stack area needed by the function or is it the overhead in terms of execution speed? If 1st one is the case i am not so worried but if second one (speed) is affected, then recursive calling will not work for me.

Ancient Dragron mentioned strtok...I would explore that option...Here's what I came up with in a few minutes of playing around...

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

char ch[] = "this is a string of text for testing purposes and only for testing";

int main(int argc, char**argv)
{
	int i = 0;
	int len = strlen(ch);
	char *str = &ch[0];
	char *end = &ch[len];
	char *endpos = end;

	while (end-- != str)
	{
		if (end[0] == ' ')
		{
			for (i = 1; i < (endpos - end); ++i)
			{
				fprintf(stdout, "%c", end[i]);
			}
		endpos = end;
		fputc(' ', stdout);		
		}
	}
	fputs("\n", stdout);
	exit(EXIT_SUCCESS);
}

As i did in my first post, i meant by using recursive function calls.... when we call a function recursively , it will automatically push the local variables to stack right?

Wrong. The variables are already on the stack. The only thing pushed onto the stack is the return address of the calling function. On function entry the compiler will generate the code needed to reserve stack space for the function's local variables plus a few that the compiler itself might need for temporary storage.

in my case , to print in reverse order, i thought recursion would be suffiecient

It would probably work, but it won't be the most efficient in either speed or memory (stack) requirements. The most efficient method is to use a loop without recursion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.