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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    `char *swap;` is just a pointer... you can't store any data in it.... – Support Ukraine Oct 31 '22 at 07:00
  • @SupportUkraine I thought pointers are memory addresses to values, which can be accessed by dereferencing. Is this wrong? – not_castillo Oct 31 '22 at 07: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];` – Support Ukraine Oct 31 '22 at 07: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... – Support Ukraine Oct 31 '22 at 07:06
  • @SupportUkraine ohh... you have to initialize the pointer, I see. Thanks – not_castillo Oct 31 '22 at 07:11
  • 3
    If you your professor teaches to use `gets`, find a better professor. Read https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used or any link about gets you can find – n. m. could be an AI Oct 31 '22 at 07:23
  • Write your own `mystrcpy using only pointers and a while loop (3-4 lines of code). Then use `mystrcpy`. – Jabberwocky Oct 31 '22 at 07:52
  • [Or just do something like this](https://godbolt.org/z/dGv5cjYfn). Note that assumes what your code assumes: that both string bases refer to sufficient memory to hold the *longest* of the two strings. Ideally you ensure that by providing max-size arguments as well. – WhozCraig Oct 31 '22 at 08:48
  • Logically you can not swap two arrays of different sizes without expanding the smaller one. If your b > a, your code that does "assign b to a" should crash. Apologies if you are not looking for swapping "strings" of different sizes. PS: should this not be called `string_swapper`? – Chef Gladiator Jan 21 '23 at 20:29
  • I applaud your attempt, but please consider ROI in your time. (very) Many have tried to write "faster" string functions. Very few (if any) have succeeded. Take this as an "obvious" example of "slow" and try to beat it. https://godbolt.org/z/W7ab1aqEb . Using `https://github.com/sheredom/ubench.h `, ready for you to add your string swap. Benchmark. – Chef Gladiator Jan 21 '23 at 21:12

1 Answers1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • that works only if both strings are of the same length (not the `strlen`) – Chef Gladiator Jan 21 '23 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"; – Vlad from Moscow Jan 21 '23 at 18:07
  • https://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 :) – Chef Gladiator Jan 21 '23 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. – Vlad from Moscow Jan 21 '23 at 20:18
  • @ChefGladiator As for your code then it is entirely wrong. See the function declaration in the question. – Vlad from Moscow Jan 21 '23 at 20:19
  • https://godbolt.org/z/of71Tj6z5 -- now please see your code failing :) – Chef Gladiator Jan 21 '23 at 20:22
  • @ChefGladiator One more the presented by you example has nothing common with my code. The requirement of the function is the arrays that store string must be large enough to be able to swap the strings. Consider for example standard function strcpy. It is the task of the user of the function to guarantee that the destination array is large enough to store the source string. This is valid for all standard string functions. The same requirement is present for the function in the question. – Vlad from Moscow Jan 21 '23 at 20:25
  • @ChefGladiator As for your code then it is obvious that the second array is not large enough to store the string stored in the second array. So calling the function invokes undefined behavior. – Vlad from Moscow Jan 21 '23 at 20:28
  • @ChefGladiator For example in the section "7.23.1 String function conventions" of the C Standard there is written "If an array is accessed beyond the end of an object, the behavior is undefined" So it is the task of the user of such a function to guarantee that used arrays are large enough to store strings. – Vlad from Moscow Jan 21 '23 at 20:34
  • @ChefGladiator Consider for example one more standard function strcat. Again the user shall guarantee that the destination array is large enough to be able to store the appended string. – Vlad from Moscow Jan 21 '23 at 20:40