inverting a string

hi,
i'm quite new in C programming and i have a question regarding strings:
how do i invert a string without using character arrays? does this mean i'm goin to use pointers? how?
thanks in advance
«1

Comments

  • : hi,
    : i'm quite new in C programming and i have a question regarding strings:
    : how do i invert a string without using character arrays? does this mean i'm goin to use pointers? how?
    : thanks in advance
    :

    This is quite easy. First allocate buffer that is as long as original string (remember to store pointer to buffer). Set char pointer in front of new buffer and another char pointer at the end of original string. Then copy characters one by one inside a loop, push pointer to buffer one char forward and pointer to string one char backward at every round until whole string is copied. Finally copy buffer's content to original string and free buffer.

    I'm sure this isn't the best way to do this but maybe the most simple.
  • [green]...[/green]
    [code]
    char *aString = "This is a string to be reversed";
    printf("Reversed String : %s",strrev(aString));
    [/code]
    [size=2][red]

    [b]Bikram[/b][/red]
    [blue]http://www.geocities.com/nv5050[/blue]
    [red]**************************************[/red][/size]

  • just swap characters - you don't need any extra memory:

    [code]
    char buff[] = "some string";
    char temp, *lo=buff, *hi=buff+strlen(buff)-1;
    while(lo != hi)
    {
    temp = *lo;
    *lo = *hi;
    *hi = temp;
    ++lo; --hi;
    }
    [/code]
  • : [green]...[/green]
    : [code]
    : char *aString = "This is a string to be reversed";
    : printf("Reversed String : %s",strrev(aString));
    : [/code]
    : [size=2][red]
    :
    : [b]Bikram[/b][/red]
    : [blue]http://www.geocities.com/nv5050[/blue]
    : [red]**************************************[/red][/size]
    :
    :

    what header is strrev from? it doesnt seem to belong to the standard library.
  • : just swap characters - you don't need any extra memory:
    :
    : [code]
    : char buff[] = "some string";
    : char temp, *lo=buff, *hi=buff+strlen(buff)-1;
    : while(lo != hi)
    : {
    : temp = *lo;
    : *lo = *hi;
    : *hi = temp;
    : ++lo; --hi;
    : }
    : [/code]
    :

    temp is extra memory :P

    You can use swap algorithms which don't use even a temporary variable. ;)


    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • :
    : what header is strrev from? it doesnt seem to belong to the standard library.
    :

    Hi,

    The strrev()-function is in string.h. This code compliled on both M$ Visual C++ and Dev-C++.

    [code]
    #include
    #include

    using namespace std;

    int main ( int argc, char* argv[] )
    {
    cout << "string reversed: " << strrev(argv[1]) << endl;
    return 0;
    }
    [/code]

    But this code didn't complile on linux debian with GNU's g++, nor on
    HP-UX 11.00 with aCC. This pretty much makes it a Windows-specific function.


    Teemu Hellman
    C++/CORBA specialist@Yomi Applications, Finland
    teemu.hellman@yomi.com

  • [code]
    char buff[] = "some string";
    for(int i = 0, j = strlen(buff) - 1; i < j; i++, j--)
    buff[i] ^= buff[j] ^= buff[i] ^= buff[j];
    [/code]

    : : just swap characters - you don't need any extra memory:
    : :
    : : [code]
    : : char buff[] = "some string";
    : : char temp, *lo=buff, *hi=buff+strlen(buff)-1;
    : : while(lo != hi)
    : : {
    : : temp = *lo;
    : : *lo = *hi;
    : : *hi = temp;
    : : ++lo; --hi;
    : : }
    : : [/code]
    : :
    :
    : temp is extra memory :P
    :
    : You can use swap algorithms which don't use even a temporary variable. ;)
    :
    :
    : "We can't do nothing and think someone else will make it right."
    : -Kyoto Now, Bad Religion
    :
    :

  • now you've spoiled it! :P
  • : [code]
    : char buff[] = "some string";
    : for(int i = 0, j = strlen(buff) - 1; i < j; i++, j--)
    : buff[i] ^= buff[j] ^= buff[i] ^= buff[j];
    : [/code]


    isnt that illegal since the same variable is changed twice between 2 sequence points?





    :
    : : : just swap characters - you don't need any extra memory:
    : : :
    : : : [code]
    : : : char buff[] = "some string";
    : : : char temp, *lo=buff, *hi=buff+strlen(buff)-1;
    : : : while(lo != hi)
    : : : {
    : : : temp = *lo;
    : : : *lo = *hi;
    : : : *hi = temp;
    : : : ++lo; --hi;
    : : : }
    : : : [/code]
    : : :
    : :
    : : temp is extra memory :P
    : :
    : : You can use swap algorithms which don't use even a temporary variable. ;)
    : :
    : :
    : : "We can't do nothing and think someone else will make it right."
    : : -Kyoto Now, Bad Religion
    : :
    : :
    :
    :

  • : : [code]
    : : char buff[] = "some string";
    : : for(int i = 0, j = strlen(buff) - 1; i < j; i++, j--)
    : : buff[i] ^= buff[j] ^= buff[i] ^= buff[j];
    : : [/code]
    :
    :
    : isnt that illegal since the same variable is changed twice between 2 sequence points?
    :

    I dont know what you mean, please elaborate?

  • chapter 5 point 4 of ISO/IEC 14882:1998(E) reads:

    Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual
    expressions, and the order in which side effects take place, is unspecified.53) Between the previous
    and next sequence point a scalar object shall have its stored value modified at most once by the evaluation
    of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
    The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full
    expression; otherwise the behavior is undefined. [Example:
    i = v[i++]; // the behavior is unspecified
    i = 7, i++, i++; // i becomes 9
    i = ++i + 1; // the behavior is unspecified
    i = i + 1; // the value of i is incremented
    end example]





    : : : [code]
    : : : char buff[] = "some string";
    : : : for(int i = 0, j = strlen(buff) - 1; i < j; i++, j--)
    : : : buff[i] ^= buff[j] ^= buff[i] ^= buff[j];
    : : : [/code]
    : :
    : :
    : : isnt that illegal since the same variable is changed twice between 2 sequence points?
    : :
    :
    : I dont know what you mean, please elaborate?
    :
    :

  • I read the text twice but still cannot make head or tail out of it. Maybe because I have no idea what a sequence point is? :)

    On any rate, for me, the code works, has been working on every C and C++ compiler I ever used, hence I'm happy and dandy with it. I don't care too much for standards I'm afraid.

    That does not mean the programmer who actually writes such code in a commercial application should not be shot.

    : chapter 5 point 4 of ISO/IEC 14882:1998(E) reads:
    :
    : Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual
    : expressions, and the order in which side effects take place, is unspecified.53) Between the previous
    : and next sequence point a scalar object shall have its stored value modified at most once by the evaluation
    : of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
    : The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full
    : expression; otherwise the behavior is undefined. [Example:
    : i = v[i++]; // the behavior is unspecified
    : i = 7, i++, i++; // i becomes 9
    : i = ++i + 1; // the behavior is unspecified
    : i = i + 1; // the value of i is incremented
    : end example]
    :
    :
    :
    :
    :
    : : : : [code]
    : : : : char buff[] = "some string";
    : : : : for(int i = 0, j = strlen(buff) - 1; i < j; i++, j--)
    : : : : buff[i] ^= buff[j] ^= buff[i] ^= buff[j];
    : : : : [/code]
    : : :
    : : :
    : : : isnt that illegal since the same variable is changed twice between 2 sequence points?
    : : :
    : :
    : : I dont know what you mean, please elaborate?
    : :
    : :
    :
    :

  • thats chapter 1.9

    [intro.execution] 1.9 Program execution
    1 The semantic descriptions in this International Standard define a parameterized nondeterministic abstract
    machine. This International Standard places no requirement on the structure of conforming implementations.
    In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming
    implementations are required to emulate (only) the observable behavior of the abstract machine as
    explained below.5)
    2 Certain aspects and operations of the abstract machine are described in this International Standard as
    implementationdefined
    (for example, sizeof(int)). These constitute the parameters of the abstract
    machine. Each implementation shall include documentation describing its characteristics and behavior in
    these respects. Such documentation shall define the instance of the abstract machine that corresponds to
    that implementation (referred to as the corresponding instance below).
    3 Certain other aspects and operations of the abstract machine are described in this International Standard as
    unspecified (for example, order of evaluation of arguments to a function). Where possible, this International
    Standard defines a set of allowable behaviors. These define the nondeterministic aspects of the
    abstract machine. An instance of the abstract machine can thus have more than one possible execution
    sequence for a given program and a given input.
    4 Certain other operations are described in this International Standard as undefined (for example, the effect of
    dereferencing the null pointer). [Note: this International Standard imposes no requirements on the behavior
    of programs that contain undefined behavior. ]
    5 A conforming implementation executing a wellformed
    program shall produce the same observable behavior
    as one of the possible execution sequences of the corresponding instance of the abstract machine with
    the same program and the same input. However, if any such execution sequence contains an undefined
    operation, this International Standard places no requirement on the implementation executing that program
    __________________
    4) The acronym POD stands for plain old data.
    5) This provision is sometimes called the asif
    rule, because an implementation is free to disregard any requirement of this International
    Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior
    of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that its value is not used
    and that no side effects affecting the observable behavior of the program are produced.
    5
    ISO/IEC 14882:1998(E) ISO/IEC
    1.9 Program execution 1 General
    with that input (not even with regard to operations preceding the first undefined operation).
    6 The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and
    calls to library I/O functions.6)
    7 Accessing an object designated by a volatile lvalue (3.10), modifying an object, calling a library I/O
    function, or calling a function that does any of those operations are all side effects, which are changes in the
    state of the execution environment. Evaluation of an expression might produce side effects. At certain
    specified points in the execution sequence called sequence points, all side effects of previous evaluations
    shall be complete and no side effects of subsequent evaluations shall have taken place.7)
    8 Once the execution of a function begins, no expressions from the calling function are evaluated until execution
    of the called function has completed.8)
    9 When the processing of the abstract machine is interrupted by receipt of a signal, the values of objects with
    type other than volatile sig_atomic_t are unspecified, and the value of any object not of
    volatile sig_atomic_t that is modified by the handler becomes undefined.
    10 An instance of each object with automatic storage duration (3.7.2) is associated with each entry into its
    block. Such an object exists and retains its laststored
    value during the execution of the block and while the
    block is suspended (by a call of a function or receipt of a signal).
    11 The least requirements on a conforming implementation are:
    At sequence points, volatile objects are stable in the sense that previous evaluations are complete and
    subsequent evaluations have not yet occurred.
    At program termination, all data written into files shall be identical to one of the possible results that
    execution of the program according to the abstract semantics would have produced.
    The input and output dynamics of interactive devices shall take place in such a fashion that prompting
    messages actually appear prior to a program waiting for input. What constitutes an interactive device is
    implementationdefined.
    [Note: more stringent correspondences between abstract and actual semantics may be defined by each
    implementation. ]
    12 A fullexpression
    is an expression that is not a subexpression of another expression. If a language construct
    is defined to produce an implicit call of a function, a use of the language construct is considered to be an
    expression for the purposes of this definition.
    13 [Note: certain contexts in C++ cause the evaluation of a fullexpression
    that results from a syntactic construct
    other than expression (5.18). For example, in 8.5 one syntax for initializer is
    ( expressionlist
    )
    but the resulting construct is a function call upon a constructor function with expressionlist
    as an argument
    list; such a function call is a fullexpression.
    For example, in 8.5, another syntax for initializer is
    = initializerclause
    but again the resulting construct might be a function call upon a constructor function with one assignmentexpression
    as an argument; again, the function call is a fullexpression.
    ]
    __________________
    6) An implementation can offer additional library I/O functions as an extension. Implementations that do so should treat calls to those
    functions as observable behavior as well.
    7) Note that some aspects of sequencing in the abstract machine are unspecified; the preceding restriction upon side effects applies to
    that particular execution sequence in which the actual code is generated. Also note that when a call to a library I/O function returns,
    the side effect is considered complete, even though some external actions implied by the call (such as the I/O itself) may not have completed
    yet.
    8) In other words, function executions do not interleave with each other.
    6
    ISO/IEC ISO/IEC 14882:1998(E)
    1 General 1.9 Program execution
    14 [Note: the evaluation of a fullexpression
    can include the evaluation of subexpressions that are not lexically
    part of the fullexpression.
    For example, subexpressions involved in evaluating default argument expressions
    (8.3.6) are considered to be created in the expression that calls the function, not the expression that
    defines the default argument. ]
    15 [Note: operators can be regrouped according to the usual mathematical rules only where the operators really
    are associative or commutative.9) For example, in the following fragment
    int a, b;
    /*...*/
    a = a + 32760 + b + 5;
    the expression statement behaves exactly the same as
    a = (((a + 32760) + b) + 5);
    due to the associativity and precedence of these operators. Thus, the result of the sum (a + 32760) is
    next added to b, and that result is then added to 5 which results in the value assigned to a. On a machine in
    which overflows produce an exception and in which the range of values representable by an int is
    [ 32768,+32767], the implementation cannot rewrite this expression as
    a = ((a + b) + 32765);
    since if the values for a and b were, respectively, 32754 and 15, the sum a + b would produce an
    exception while the original expression would not; nor can the expression be rewritten either as
    a = ((a + 32765) + b);
    or
    a = (a + (b + 32765));
    since the values for a and b might have been, respectively, 4 and 8 or 17 and 12. However on a
    machine in which overflows do not produce an exception and in which the results of overflows are
    reversible, the above expression statement can be rewritten by the implementation in any of the above ways
    because the same result will occur. ]
    16 There is a sequence point at the completion of evaluation of each fullexpression10).
    17 When calling a function (whether or not the function is inline), there is a sequence point after the evaluation
    of all function arguments (if any) which takes place before execution of any expressions or statements in
    the function body. There is also a sequence point after the copying of a returned value and before the execution
    of any expressions outside the function11). Several contexts in C++ cause evaluation of a function
    call, even though no corresponding function call syntax appears in the translation unit. [Example: evaluation
    of a new expression invokes one or more allocation and constructor functions; see 5.3.4. For another
    example, invocation of a conversion function (12.3.2) can arise in contexts in which no function call syntax
    appears. ] The sequence points at functionentry
    and functionexit
    (as described above) are features of the
    function calls as evaluated, whatever the syntax of the expression that calls the function might be.
    18 In the evaluation of each of the expressions
    a && b
    a || b
    a ? b : c
    a , b
    using the builtin
    meaning of the operators in these expressions (5.14, 5.15, 5.16, 5.18), there is a sequence
    __________________
    9) Overloaded operators are never assumed to be associative or commutative.
    10) As specified in 12.2, after the "endoffullexpression"
    sequence point, a sequence of zero or more invocations of destructor functions
    for temporary objects takes place, usually in reverse order of the construction of each temporary object.
    11) The sequence point at the function return is not explicitly specified in ISO C, and can be considered redundant with sequence
    points at fullexpressions,
    but the extra clarity is important in C++. In C++, there are more ways in which a called function can terminate
    its execution, such as the throw of an exception.
    7
    ISO/IEC 14882:1998(E) ISO/IEC
    1.9 Program execution 1 General
    point after the evaluation of the first expression12).


    : chapter 5 point 4 of ISO/IEC 14882:1998(E) reads:
    :
    : Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual
    : expressions, and the order in which side effects take place, is unspecified.53) Between the previous
    : and next sequence point a scalar object shall have its stored value modified at most once by the evaluation
    : of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
    : The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full
    : expression; otherwise the behavior is undefined. [Example:
    : i = v[i++]; // the behavior is unspecified
    : i = 7, i++, i++; // i becomes 9
    : i = ++i + 1; // the behavior is unspecified
    : i = i + 1; // the value of i is incremented
    : end example]
    :
    :
    :
    :
    :
    : : : : [code]
    : : : : char buff[] = "some string";
    : : : : for(int i = 0, j = strlen(buff) - 1; i < j; i++, j--)
    : : : : buff[i] ^= buff[j] ^= buff[i] ^= buff[j];
    : : : : [/code]
    : : :
    : : :
    : : : isnt that illegal since the same variable is changed twice between 2 sequence points?
    : : :
    : :
    : : I dont know what you mean, please elaborate?
    : :
    : :
    :
    :

  • Maybe I got this backwards, but isn't each evaluation of ^= a sequence point? that would make only one change, not two.

    : : : : : [code]
    : : : : : char buff[] = "some string";
    : : : : : for(int i = 0, j = strlen(buff) - 1; i < j; i++, j--)
    : : : : : buff[i] ^= buff[j] ^= buff[i] ^= buff[j];
    : : : : : [/code]
    : : : :
    : : : :
    : : : : isnt that illegal since the same variable is changed twice between 2 sequence points?

  • : Maybe I got this backwards, but isn't each evaluation of ^= a sequence point? that would make only one change, not two.


    if you had read what i posted youd know that sequence points can only be before and after an evaluation. since there are no function calls or || ?: , && in it, i think there is one sequence point before and one after this "full expression".
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories