Thread: Reversing string ( not words) - too much problem

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    15

    Reversing string ( not words) - too much problem

    I am trying to write a program in C to reverse a string .

    Example - This is America
    output - America is This

    I have thought of two ways of doing this question

    1) using 2D arrays ( no pointer)
    2) Using Strings and pointer

    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
    
    
    char a[10][3];
    int i,j,beg,end,count = 0,temp;
    printf("enter the string");
    for(i=0;i<10;i++)
    {
        
    
    
        
        for(j=0;j<3;j++)
        {
            
            scanf("%s",a[i][j]);
            
        }
    }
    
    
    beg = 0;
    end = 3;
    for(i=0;i<10;i++)
    {
        
    
    
    if(beg!=end)
    
    
    {
        temp = a[i][beg];
        a[i][beg] = a[i][end];
        a[i][end] = temp;
        
    }
    
    
    beg++;
    end--;
    }
    
    
    printf("the reverse string is");
    for(i=0;i<10;i++)
    {
        
        for(j=0;j<3;j++)
        {
            printf("%s",a[i][j]);
        }
    }
        
        
    }


    can anyone tell what i am doing wrong ?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    What is the output that you are getting? How is it incorrect?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    15
    When it ask to enter string . I write it and hit enter then it say "Untitle1.cpp stopped working"

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    It looks like your array sizes are backwards. To have an array of 3 strings with max length 10, you'd declare your array as
    Code:
    char a[3][10];
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    On first pass you are trying to enter the entire string into a[0][0].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in reversing words in an Array
    By Justin Page in forum C Programming
    Replies: 8
    Last Post: 07-22-2012, 12:18 AM
  2. Reversing String Problem
    By Bennie98 in forum C Programming
    Replies: 14
    Last Post: 10-22-2010, 08:24 AM
  3. Reversing words in a string
    By rocketman03 in forum C Programming
    Replies: 4
    Last Post: 09-07-2009, 01:23 AM
  4. Reversing words...noob
    By shardin in forum C Programming
    Replies: 5
    Last Post: 08-23-2007, 11:11 AM
  5. Reversing words
    By jlf029 in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2005, 09:48 PM