tags:

views:

162

answers:

4

heres what i did, i just have ne error that i cant figure out.

int mystrlen(char string[])
{
 char string1[LENGHT], string2[LENGHT];
 int len1, len2;
 char newstring[LENGHT*2];

 printf("enter first string:\n");
 len1 = mystrlen(string1);
 printf("enter second string:\n");
 len2 = mystrlen(string2);

 if(len1 == EOF || len2 == EOF)
  exit(1);

 strcpy(newstring, string1);
 strcat(newstring, string2);

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

 return 0;
A: 

You recursively call the same function on all branches. It ain't gonna work - the program will incur stack overflow and most likely crash big time.

You need to structure the program somehow like this:

int mystrlen(char string[])
{
    //compute length here
}

void testStuff()
{
    //your code here
}
sharptooth
A: 

Try to read your program using Pseudocode - It will probably be easier to understand then :)

  1. Declare variables string1, string2, len1, len2, newstring
  2. Print a message
  3. 3 Go to 1
    3.1. Declare variables string1, string2, len1, len2, newstring
    3.2. Print a message
    3.3. Go to 3.1
    ... 3.3.1 Declare variables string1, string2, len1, len2, newstring
    ... 3.3.2 .....

You see the error? :)

cwap
A: 

Sorrt the other post wasnt clear.

Write a function int mystrlen(char *s) that returns the number of characters in string s.
        e.g., char s[]=”program”, strlen(s) is 7.
  You can not call strlen() function in your program.

heres whats i have.

#define LENGHT 80


extern int getline(char *s);

int main()
{
    char string1[LENGHT];
    int len1, len2;
    char newstring[LENGHT*2];

    printf("enter first string:\n");
    len1 = getline(string1);
    printf("enter second string:\n");
    len2 = getline(string2);

    if(len1 == EOF || len2 == EOF)
     exit(1);

    strcpy(newstring, string1);
    strcat(newstring, string2);

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

    return 0;
}
henry
Please edit your question next time. People will not look through all all the answers to understand your question. They will read the question. That's why you CAN edit the question.
Thorsten Dittmar
+2  A: 

Cheating, but follows your rules...

int mystrlen( char *s)
{
    return strchr( s, '\0') - s;
}
tomlogic