Thread: Reverse word (Program crash)

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    14

    Question Reverse word (Program crash)

    Code:
    #include <stdio.h>
    
    
    int main ()
    
    
    {
        int i;
        char reversed_title[32];
        char title[32] = "programming concepts and design";
    
    
        for (i=0;i<=31;i++)
        {
            title[i]=reversed_title[31-i];
    
    
        }
    
    
        for (i=0;i<=31;i++)
    
    
        {
            printf("%s", reversed_title[i]);
        }
    
    
    return 0;
    
    
    }
    Why my program crash immediately after i run it?
    Last edited by CruelSoulz; 07-18-2012 at 09:25 AM.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    relook at this line 'title[i]=reversed_title[31-i];'. what is in reversed_title at that point?

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    14
    The first word of title will be the last word of reversed_title, and the last word of title will be the first word of reversed_title.
    Sorry for bad English.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by CruelSoulz View Post
    Code:
        for (i=0;i<=31;i++)
        {
            title[i]=reversed_title[31-i];
    
    
        }
    reversed_title is uninitialized so this puts garbage data into the title array.

    Quote Originally Posted by CruelSoulz View Post
    Code:
         for (i=0;i<=31;i++)
    
    
        {
            printf("%s", reversed_title[i]);
        }
    printf is expecting a pointer to char here, but reversed_title[i] is not a pointer to char. It is a char. This is probably the cause of the crash.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    14
    Changed
    Code:
    %s to %c
    and
    Code:
    {tittle[i]=reversed_tittle[31-i];} to {reversed_tittle[31-i]=tittle[i];}
    and it worked !! ... Thanks dude ~~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to reverse a string word by word?
    By SuperMiguel in forum C Programming
    Replies: 22
    Last Post: 03-29-2012, 12:40 AM
  2. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  3. Replies: 1
    Last Post: 05-30-2010, 10:22 PM
  4. my program crash?
    By vrkiller in forum C++ Programming
    Replies: 10
    Last Post: 03-04-2009, 03:03 PM
  5. reverse a word
    By hope in forum C Programming
    Replies: 1
    Last Post: 09-17-2008, 07:42 AM