Help handling strings in C

I am trying to change character string in C

That's what i

void reverse(char str[]) {
    int i = 0;

    int length;
    // Get string length
    for (i = 0; str[i] != '\0' ; ++i) {
        length = i;
    }

    char reversed[1000];

    int j;
    j = 0;
    // Reverse it
    for (j = 0; j < length ; ++j) {
        reversed[j] = str[length - j];
    }

}

I know what the reversedstring contains in the reverse order, but I'm not sure how to change the original strwithout dropping the data I need.

I also don't know how to set strin reversedwithout repeating the loop.

It would be acceptable to do one more ...

    int m;
    m = 0;

    for (m = 0; m < length ; ++m) {
        str[j] = reversed[j];
    }

Usually I would say that it is a lot of cycles, it smells, but I am also still unfamiliar with the language, so I'm not sure ...

Update

Thanks for the answers guys, and I also appreciate the changes!

I ended up moving with this ...

int main() {

 char str[] = "Reverse me!";

 int length;

 for (length = 0; str[length] != '\0'; length++) {

 }

 printf("length => %d chars\n", length);

 int j, k;
 char c;

 for (j = 0, k = length - 1; j < k; j++, k--) {
  c = str[k];    
  str[k] = str[j];
  str[j] = c;
 } 

 printf("reversed => %s\n", str);

 return 0;
}

Some things that I now know ...

  • In PHP there is strlen(). However, this has not yet been discussed in the book, plus I need to get acquainted with zero completed lines.
  • for . !

:)

+2
4

:

void reverse(char str[]) {
    int i = 0;

    int length;
    // Get string length
    for (i = 0; str[i] != '\0' ; ++i) {
        length = i;
    }

, .

size_t len = 0; // size_t is an unsigned integer that is large enough to hold the sizes
                // of the biggest things you can have (32 bits on 32 bit computer,
                // 64 bits on a 64 bit computer)
char * s = str;
while (*s) {
    len++;
    s++;
}

, , .

, strlen (#include <string.h>), char, ( ), .

len = strlen(str);

:

    char reversed[1000];

, , . - malloc, stdlib.h ( malloc.h). , , .

int * p = malloc( 8 * sizeof(int) ); // allocate an array of 8 ints
/* ... work on p ... */
free(p);
/* ... don't access the memory pointed to by p anymore ... */
p = 0;

malloc . calloc, , 0. strdup ( C, string.h), . :

char * strdup(const char * str) {
    size_t len = strlen(str);
    char * s = malloc(len+1);
    if (!s) {
        return s;
    }
    return strcpy(s,str); // This could have been memcpy since you know the size
                          // and memcpy might have been faster on many processors
}

- alloca ( C, C99). , , malloc. , , , , ( ).

:

    int j;
    j = 0;
    // Reverse it
    for (j = 0; j < length ; ++j) {
        reversed[j] = str[length - j];
    }

:

void reverse_in_place(char * str, size_t len) {
   size_t i, j;
   for (i = 0, j = len - 1; i < j ; i++, j--) {
        char a = str[i];
        char z = str[j];
        str[i] = z;
        str[j] = a;
   }
}

, . char .

+1

. :

// taken from The C Programming Language
//    by Brian Kernighan and Dennis Ritchie (K&R)
void reverse(char s[])
{
      int c, i, j;

      for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
         c = s[i];
         s[i] = s[j];
         s[j] = c;
      }
}

, strlen . , string.h.

.

+4

, , . , , , .

:

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

// The pointer version.
void reverse1 (char *str) {
    char t;                      // Temporary char for swapping.
    char *s = str;               // First character of string.
    char *e = &(s[strlen(s)-1]); // Last character of string.

    // Swap first and last character the move both pointers
    // towards each other. Stop when they meet or cross.
    while (s < e) {
        t = *s;
        *s++ = *e;
        *e-- = t;
    }
}

// The array version.
void reverse2 (char *str) {
    char t;                // Temporary char for swapping.
    int s = 0;             // First character of string.
    int e = strlen(str)-1; // Last character of string.

    // Swap first and last character the move both pointers
    // towards each other. Stop when they meet or cross.
    while (s < e) {
        t = str[s];
        str[s++] = str[e];
        str[e--] = t;
    }
}

 

int main (void) {
    char x[] = "This is a string for reversing.";
    printf ("Original: [%s]\n", x);
    reverse1 (x);
    printf ("Reversed: [%s]\n", x);
    reverse2 (x);
    printf ("   Again: [%s]\n", x);
    return 0;
}

:

Original: [This is a string for reversing.]
Reversed: [.gnisrever rof gnirts a si sihT]
   Again: [This is a string for reversing.]
+4

C

#include<stdio.h>
char *srcptr = "Hello!";
char *destptr;
unsigned int length = 0;
void main(void)
{
   while(*(ptr++) != '\0')
   {
       length++;
   }
   //at the end of while loop, pointer points to end of string 
   while(length--)
   {
        *destptr++ = *ptr--;
   }
    //append null at the end
    *destptr = '\0';
    printf("%s",ptr);
 }
0

All Articles