Reverse a String in C - Solve Segmentation Fault

In summary, the conversation is about a code that results in a segmentation fault when run on Linux. The person is seeking help to understand why this is happening. The conversation also includes a discussion about the position of the terminating null character in a string and how it can lead to a segmentation fault.
  • #1
carl123
56
0
1.JPG

2.JPG

This is what I came up with but I keep getting segmentation fault whenever I run it in linux. Not sure why. Any help would be appreciated. Thanks
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
    int i, childpid;

    for (i = strlen(argv[1]); i >= 0; i--) {
        if (fork() == 0) {
            printf("%c\n",argv[1][i]);
            break;
        }
    }

    return 0;
}
 
Mathematics news on Phys.org
  • #2
carl123 said:
i = strlen(argv[1])
This is the position of the terminating null character, not the last character of the string.
 
  • Like
Likes carl123
  • #3
Orodruin said:
This is the position of the terminating null character, not the last character of the string.
Thanks for your reply. Pls could you explain further. Not sure what you mean
 
  • #4
if you have a 10 character string then valid indexes are 0-9 but strlen returns a 10 so your first loop iteration is referencing an out of bounds element of the string which can result in a segmentation fault but not always.
 
  • #5
Please note: @CAF123 this post is missing a homework template. Since there are answers it will stay as is.
 
  • #6
  • #7
Thanks for the correction. Since the thread died and @CAF123 straightened me out, let's close this thread.
 

1. How do I reverse a string in C?

To reverse a string in C, you can use the strrev() function from the string.h library.

2. Why am I getting a segmentation fault when trying to reverse a string in C?

A segmentation fault can occur when you try to access memory that is outside of the bounds of what is allocated to your program. This can happen if you do not properly allocate memory for your string or if you try to access memory beyond the length of your string.

3. How can I avoid getting a segmentation fault when reversing a string in C?

To avoid a segmentation fault when reversing a string in C, make sure to properly allocate memory for your string and to only access memory within the bounds of your string's length.

4. Can I reverse a string in place in C?

Yes, you can reverse a string in place in C by using a temporary variable to swap the characters at opposite ends of the string until you reach the middle.

5. Is there a more efficient way to reverse a string in C?

Yes, there are multiple ways to reverse a string in C that may be more efficient depending on the size and complexity of the string. Some other methods include using pointers, recursion, or a stack data structure.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • General Math
Replies
2
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
891
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
949
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top