0

this program is meant to take two strings and call a function that copies string 1 into string2, and vice versa, while fulfilling the following criteria:

  • Uses pointers
  • Uses only while loops
  • does not use strcpy();

This is what I have, but nothing shows up. How can I fix this?

#include<stdio.h>

void string_copier(char *a, char *b);

int main(){
    char string1[100], string2[100];
    
    /*read strings*/
    printf("Enter a string less than 100 characters.\n");
    gets(string1);
    //scanf("%s", &string1);
    printf("Enter another string less than 100 characters.\n");
    gets(string2);
    //scanf("%s", &string2);
    
    /*call function*/
    string_copier(string1, string2);
    
    /*print new strings*/
    printf("%s\n", string1);
    printf("%s\n", string2);
}

void string_copier(char *a, char *b){
    int i = 0;
    char *swap;

    /*assign string a to swap variable*/
    while(*(a + i) != '\0'){
        *(swap + i) = *(a + i);
        i++;
    } 
    *(swap + i) = '\0';
    
    /*redefine i to use in next while loop*/
    i = 0;
    
    /*assign b to a*/
    while(*(b + i) != '\0'){
        *(a + i) = *(b + i);
        i++;
    }
    *(a + i) = '\0';
    
    i = 0;
    
    /*assign swap to b*/
    while(*(swap + i) != '\0'){
        *(b + i) = *(swap + i);
        i++;
    }
    *(b + i) = '\0';
}

Thank you.

10
  • 1
    char *swap; is just a pointer... you can't store any data in it....
    – 4386427
    Commented Oct 31, 2022 at 7:00
  • @SupportUkraine I thought pointers are memory addresses to values, which can be accessed by dereferencing. Is this wrong? Commented Oct 31, 2022 at 7:03
  • 1
    A pointer must point to some memory. Your swap is just an uninitialized pointer so it doesn't point to valid memory. One thing you can do is swap = malloc(100); but you can also just make it an array like char swap[100];
    – 4386427
    Commented Oct 31, 2022 at 7:05
  • 1
    That said... All you need is actually char swap; and then swap the characters one by one instead "copying" the whole string first...
    – 4386427
    Commented Oct 31, 2022 at 7:06
  • 3
    If you your professor teaches to use gets, find a better professor. Read stackoverflow.com/questions/1694036/… or any link about gets you can find Commented Oct 31, 2022 at 7:23

1 Answer 1

0

For starters the function gets is unsafe and is not supported by the C Standard. Instead use either fgets or scanf.

The pointer swap is uninitialized and has an indeterminate value

char *swap;

So the following code within the function where the pointer is dereferenced invokes undefined behavior.

Also using expressions like this *(a + i) with the auxiliary variable i is the same as using the subscript operator a[i] that is the pointers themselves in the function are not increased. But according to the assignment you need to use only pointers to access elements of the passed strings.

The function can be defined for example the following way as shown in the demonstration program below.

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

void string_swap( char *s1, char *s2 )
{
    while ( *s1 && *s2 )
    {
        char c = *s1;
        *s1++ = *s2;
        *s2++ = c;
    }

    char c = *s1;
    *s1 = *s2;
    *s2 = c;

    if (*s1 != '\0')
    {
        while (*s1 != '\0')
        {
            *++s1 = *++s2;
        }
    }
    else
    {
        while (*s2 != '\0')
        {
            *++s2 = *++s1;
        }
    }
}

int main( void )
{
    char s1[10] = "Hello";
    char s2[10] = "everybody";

    printf( "%zu: %s\n", strlen( s1 ), s1 );
    printf( "%zu: %s\n", strlen( s2 ), s2 );

    string_swap( s1, s2 );

    printf( "%zu: %s\n", strlen( s1 ), s1);
    printf( "%zu: %s\n", strlen( s2 ), s2 );

    string_swap( s1, s2 );

    printf( "%zu: %s\n", strlen( s1 ), s1 );
    printf( "%zu: %s\n", strlen( s2 ), s2 );
}

The program output is

5: Hello
9: everybody
9: everybody
5: Hello
5: Hello
9: everybody

As you can see the function itself does not use any C standard string function.

10
  • that works only if both strings are of the same length (not the strlen) Commented Jan 21, 2023 at 17:58
  • @ChefGladiator It is obvious that if you want to swap two strings then the arrays that store the strings must be large enough. The strings themselves can have different lengths. So your comment does not make sense. See this code snippet if (*s1 != '\0') { while (*s1 != '\0') { *++s1 = *++s2; } } else { while (*s2 != '\0') { *++s2 = *++s1; } } Moreover in the demonstration program these arrays contain strings of different lengths char s1[10] = "Hello"; char s2[10] = "everybody"; Commented Jan 21, 2023 at 18:07
  • godbolt.org/z/x3bYGseo8 -- one possible correct solution. You are swapping char arrays of the same size. string length is not what I am talking about. As I said in my previous comment. It seems your comment does not make sense :) Commented Jan 21, 2023 at 20:14
  • @ChefGladiator Sizes of arrays are not important except they have to store the swapped strings. The code in my answer does not swap arrays. It swaps strings. See the demonstration program in my answer. The function knows nothing about arrays that store strings. Commented Jan 21, 2023 at 20:18
  • @ChefGladiator As for your code then it is entirely wrong. See the function declaration in the question. Commented Jan 21, 2023 at 20:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.