1

for my program I need to add char(char) to string(char *) without using standard library or IO functions. For example:

char *s = "This is GOO";
char c = 'D';

s = append(s, c);

and s would produce string "This is GOOD". Is there some proper way to manipulate arrays in order to achieve this? Also it would be sufficient to produce string out of number of characters. I'm pretty sure I can use malloc, not positive though...

char * app(char* s, char c){
    char *copy;
    int l = strlen_(s);
    copy = malloc(l+1);
    copy = s;
    copy[l] = c;
    copy[l+1] = '\0';
    return copy;
}

can't use strcpy

2 Answers 2

1

Without giving away the answer, since this sounds like classwork, here's what you want to do at a high level:

  1. Determine the length of the string, i.e. find the '\0' terminator.
  2. Allocate a new char array which is one character longer.
  3. Copy the old string to the new string.
  4. Add the new character on at the end.
  5. Make sure there's a '\0' terminator at the end of the new string.

(If you're allowed to modify the existing string then you could possibly skip steps 2 and 3. But in your example char *s = "This is GOO" has s pointing to an unmodifiable string literal, meaning you can't modify it in place and have to work with a copy.)


Comments on the code you posted:

char * app(char* s, char c) {
    char *copy;
    int l = strlen_(s);
    copy = malloc(l+1);    /* should be +2: +1 for the extra character and +1 for \0 */
    copy = s;              /* arrays must be copied item by item. need a for loop */
    copy[l] = c;
    copy[l+1] = '\0';
    return copy;
}
Sign up to request clarification or add additional context in comments.

4 Comments

s = append(s, c); implies that s is not modified by append, unless append has a truly wretched specification.
' char * app(char* s, char c){ char *copy; int l = strlen_(s); copy = malloc(l+1); copy = s; copy[l] = c; copy[l+1] = '\0'; return copy; } ' segmentation fault =/
how to make this format like a code here...? Anyway, how it is possible to copy old string to the new one without strcpy
@duskast gave me an answer and you gave me an understanding ,thanks a lot
1
#include <stdlib.h>

char *append(char *s, char c)
{
    int i = 0, j = 0;
    char *tmp;
    while (s[i] != '\0')
        i++;
    tmp = malloc((i+2) * sizeof(char));
    while (j < i)
    {
        tmp[j] = s[j];
        j++;
    }
    tmp[j++] = c;
    tmp[j] = '\0';
    return tmp;
}

int main(void)
{
    char *s = "This is Goo";
    char c = 'D';
    s = append(s, c);
    return 0;
}

2 Comments

a) use strlen b) sizeof(char) is always 1 c) use memcpy or strcpy
Oops, sorry, missed the requirement that strcpy can't be used. Still, nothing said about strlen or memcpy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.