I'm a student. My code is skipping over my printString function when I run it. Pardon the poor spacing.

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

char * reverseString(char * original);
void printString(char * string);
int main()
{
    char a[] = "this is a test \0";
    char * b = a;

    printString(a);
    reverseString(a);
    printString(a);

    char * c = malloc(80 * sizeof(char));
    printf("please enter a string:");
    scanf("%79[^\n\t]", c);

    printString(c);
    char * d = reverseString(c);
    printString(c);

}

    void printString(char * string)
    {
    printf("hi");
    printf("The string is %s\n", string);

    }

    char* reverseString(char * original)
    {

    char hold;
    char temp;
    char* end;
    int i = 0;
    while(original != '\0')
     {
    i++;
     }
    end = end +i-1;
    while(end>original)
         {
    hold = *end;
    *end = *original;
    *original = hold;
     end = end-1;
    original++;
     }


    }

Recommended Answers

All 3 Replies

at line 12-14 i think you need to change the argument type what i mean is

your

printString(chr *string)

method expects address of the variable/ pointer varible instead of passsing value of the variable while calling the method

but you called this method by passing the value directly so that is the reason you got skipping over the function call

please check by placing the bellow format of calling code from line 12-14

printString(&a);

instead of

printString(a);

modify remaining code accordingly

its works fine

let me know the status after the modifications

happy coding

method expects address of the variable/ pointer varible instead of passsing value of the variable while calling the method
but you called this method by passing the value directly so that is the reason you got skipping over the function call

That's a good guess, but you're wrong. The argument evaluates to a pointer, it already represents an address, so no address-of operator is needed in this case. The problem is that reverseString() is all borked up; it shouldn't even compile. Anyway, replace reverseString() with the following and watch the problem go away:

char *reverseString(char *original)
{
    int i, j;

    for (i = 0, j = strlen(original) - 1; i < j; i++, j--) {
        char temp = original[i];
        original[i] = original[j];
        original[j] = temp;
    }

    return original;
}

Thanks guys. It works.

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.