1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

reverse a string?

Discussion in 'C' started by senthil_a4nlabs, Aug 31, 2016.

  1. senthil_a4nlabs

    senthil_a4nlabs New Member

    Joined:
    Aug 27, 2016
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    how we reverse a string in an array give me the programme
     
  2. ahmedsabuwala12

    ahmedsabuwala12 New Member

    Joined:
    Aug 25, 2016
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
        char str[10];
        int len, i;
        printf("\n Enter a string:");
        scanf("%s", str);
        len=strlen(str);
        for(i=len-1; i>=0; i--)
        {
            printf("%c", str);
        }
        getch();
    }
    
     
  3. persysweb

    persysweb Member

    Joined:
    Aug 1, 2017
    Messages:
    70
    Likes Received:
    12
    Trophy Points:
    8
    Location:
    India
    Home Page:
    Iterative way: Initialize start and end indexes.
    start = 0, end = n-1 In a loop, swap arr[start] with arr[end] and change start and end as follows.
    start = start +1; end = end – 1

    Code:
    Code:
    #include<stdio.h>
    /* Function to reverse arr[] from start to end*/
    void rvereseArray(int arr[], int start, int end)
    {
    int temp;
    while (start < end)
    {
    temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;
    start++;
    end--;
    }
    }
    
    /* Utility that prints out an array on a line */
    void printArray(int arr[], int size)
    {
    int i;
    for (i=0; i < size; i++)
    printf("%d ", arr);
    
    printf("n");
    }
    
    /* Driver function to test above functions */
    int main()
    {
    int arr[] = {1, 2, 3, 4, 5, 6};
    printArray(arr, 6);
    rvereseArray(arr, 0, 5);
    printf("Reversed array is n");
    printArray(arr, 6);
    return 0;
    }
    Hope this help
    Thanks!
     

Share This Page