#include<stdio.h>
#include<string.h>
void recurse(char [],const char *);
int main() {
        char *charset="abcdefghij";
        recurse("",charset);
}
void recurse(char str[],const char *charset) {
    int len;
    len=strlen(str);
    strcat(str,charset[len+1]);
    if(len<strlen(charset))
        recurse(str,charset);
    printf("%s",str);
}

Like the question says, what is wrong here? This is the logic of my program
1. Keep a string called charset which I can fill any amount of numbers, characters
2. The first digit should first be passed to printf (or) any function I make, then the 1st two digits followed by first three, all the way till the the strlen of the passed string is same as that of the charset

I heard that if i use a function like with a global variable charset having A,B,C,D,etc.

void somefunction(char str[]) { //str is initialized here as "String"
    for(int i=0;i<5;i++) {
        calledfunction(strcat(str,charset[i])); // Here the string goes as "StringA"
    }
    anotherfnction(str); // Here the string goes as "String"
}

newbie at C.. do help me fix this..

Recommended Answers

All 4 Replies

I think you can not add characters to empty string passed to function in C.

This would seem to work:

#include<stdio.h>
#include<string.h>
void recurse(const char *, int);
int main() {
        const char *charset="abcdefghij";
        recurse(charset, strlen(charset));
}
void recurse(const char *charset, int len) {
    int i;
    if(len>1) recurse(charset, len-1);
    for(i=0; i < len; i++) printf("%c", charset[i]);
    printf("\n");
}

@pyTony: that logic occured to me earlier but doesnt solve the problem. Here again your printing with respect to the length rather than postpending the character to the string. Well, thanks anyways..! I will figure this out somehow :)

The original code was in PHP, converting to C is such a pain in the a** X_x. Thanks a Lot

Why not simple for loop, if recursion is not the task? I don't really get your problem. If you could give example of failure maybe I would get it.

I agree with pyTony, recursion seems like an odd way to solve this problem. However, you should be able to get your original version to work if you give it an array that can be modified (untested):

int main(void) {
    char *charset="abcdefghij";
    char buffer[100] = "";
    recurse(buffer,charset);
}

When you call recurse("", charset) the "" is a 1-byte character array in (probably) read-only memory. You can't strcat to it or modify it in any way without invoking undefined behavior.

I heard that if i use a function like with a global variable charset having A,B,C,D,etc.

\<code snipped>

You heard wrong then. strcat() modifies its first argument permanently. (This is not the case in e.g. Java, where strings are immutable and concatenation returns a newly created string.) strcat(3)

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.