3

I am new to c programming language. I am trying to reverse elements in char array. Actually, I almost reversed but there is something that I can't make it. Here is code:

void q2_reverseTheArray(char word[100]){
int lentgh=sizeof(word);
int j;
for(j=length+1; j>=0; j--){
    printf("%c", word[j]);
}

The code reverse the array but it adds another letter.

5
  • 2
    Are you sure this is the code you're using? This shouldn't compile, as you have a typo in the declaration of length; then it should crash because you start the loop by accessing word[length + 1] which is 2 indices out of range. Commented Dec 6, 2015 at 9:31
  • Your for loop conditions are wrong. Arrays are indexed from 0 to length-1. Try the following: for (j=length-1; j>0; j--) This will start at the last character and repeat until the first one. Better yet, become accustomed to using your debugger. Commented Dec 6, 2015 at 9:33
  • Yes, I am sure. I gave parameter "book". And the output was "aakoob". Commented Dec 6, 2015 at 9:35
  • To all readers: Having said "I am new to c programming language", cut this dude some slack and refrain from down-voting or close-voting the question. Commented Dec 6, 2015 at 9:36
  • Possible duplicate of Sizeof array passed as parameter Commented Dec 6, 2015 at 9:50

4 Answers 4

5

here you have a working example:

#include <stdio.h> // printf
#include <stdlib.h> // malloc, free
#include <string.h> // strlen

int main() {
  char* s = "hello";
  size_t l = strlen(s);
  char* r = (char*)malloc((l + 1) * sizeof(char));
  r[l] = '\0';
  int i;
  for(i = 0; i < l; i++) {
    r[i] = s[l - 1 - i];
  }
  printf("normal: %s\n", s);
  printf("reverse: %s\n", r);
  free(r);
}

your code was wrong in length + 1 it should say length - 1.

and you have to pay attention to the terminating '\0'.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ! I solved the problem by counting each element for finding length.
@Sahin could you please accept (and maybe upvote ;-)) an answer, so that the question appears answered. thanks.
4

The correct code is

for(j=length-1; j>=0; j--){
   printf("%c", word[j]);

This is because the string's elements are indexed from 0 to length-1. For example,

word == "Hello"
length == 5
word[0] == 'H'
word[1] == 'e'
word[2] == 'l'
word[3] == 'l'
word[4] == 'o'

2 Comments

I tried this but when I give the word[100]={"Look at Sky"}. It didn't reverse.
@Sahin try strcpy(word, "Look at Sky"} instead
0

In my experience, using strlen() instead of sizeof() is better as it removes the need to modify the result in any way, like in my example:

char *c;
int i;
scanf("%s", c);
for (i = strlen(c); i >= 0; --i) {
  printf("%c", c[i]);
}

Comments

0

Without string.h header file

main()
{
    char str1[100], str2[100];
    int i, k, j;
    scanf("%s", &str1);
    for(i=0; str1[i] != '\0'; i++ );
    j=i-1;
    for(k=0; k<=i; k++)
    {
        str2[k]=str1[j];
        j--;
    }
    for(k=0; k<i; k++)
        printf("%c", str2[k]);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.