Reverse String in C programming


Add caption
Reverse String in C  :-
The Purpose of Reverse String in C is to reverse sentences from the end to 
the beginning 



Complied On : Code::Blocks 17.05

Save :- .c file (C Program)

Created By :- Luka Yooma 


Below Is the code for the Reverse String



/**
 * C program to reverse order of words in a string
 */
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[100], reverse[100];
    int len, i, index, wordStart, wordEnd;

    printf("Enter any sentence:\n\t ");
    gets(str);

    len   = strlen(str);
    index = 0;

    // Start checking of words from the end of string
    wordStart = len - 1;
    wordEnd   = len - 1;

    while(wordStart > 0)
    {



        // If a word is found
        if(str[wordStart] == ' ')
        {
            // Add the word to the reverse string
            i = wordStart + 1;
            while(i <= wordEnd)
            {
                reverse[index] = str[i];

                i++;
                index++;
            }
            reverse[index++] = ' ';

            wordEnd = wordStart - 1;
        }

        wordStart--;
    }

    // Finally add the last word
    for(i=0; i<=wordEnd; i++)
    {
        reverse[index] = str[i];
        index++;
    }

    // Add NULL character at the end of reverse string
    reverse[index] = '\0';

    printf("Original sentence \n%s\n\n", str);
    printf("Reversed Sentence \n%s", reverse);

    return 0;

}




Thanks to all my Readers Leave a comment for any queries.......

Reverse String in C programming


Add caption
Reverse String in C  :-
The Purpose of Reverse String in C is to reverse sentences from the end to 
the beginning 



Complied On : Code::Blocks 17.05

Save :- .c file (C Program)

Created By :- Luka Yooma 


Below Is the code for the Reverse String



/**
 * C program to reverse order of words in a string
 */
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[100], reverse[100];
    int len, i, index, wordStart, wordEnd;

    printf("Enter any sentence:\n\t ");
    gets(str);

    len   = strlen(str);
    index = 0;

    // Start checking of words from the end of string
    wordStart = len - 1;
    wordEnd   = len - 1;

    while(wordStart > 0)
    {



        // If a word is found
        if(str[wordStart] == ' ')
        {
            // Add the word to the reverse string
            i = wordStart + 1;
            while(i <= wordEnd)
            {
                reverse[index] = str[i];

                i++;
                index++;
            }
            reverse[index++] = ' ';

            wordEnd = wordStart - 1;
        }

        wordStart--;
    }

    // Finally add the last word
    for(i=0; i<=wordEnd; i++)
    {
        reverse[index] = str[i];
        index++;
    }

    // Add NULL character at the end of reverse string
    reverse[index] = '\0';

    printf("Original sentence \n%s\n\n", str);
    printf("Reversed Sentence \n%s", reverse);

    return 0;

}




Thanks to all my Readers Leave a comment for any queries.......

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel