reversing words using array in c language

how to reverse words using looping in array with out he application of

Page 1 of 1

1 Replies - 2418 Views - Last Post: 18 February 2009 - 10:24 PM Rate Topic: -----

#1 leeluce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 09

reversing words using array in c language

Post icon  Posted 18 February 2009 - 09:04 PM

#include<stdio.h>
#define SIZE 26


char orig[char [],SIZE];
char reve[SIZE];


int main()
{

	char string1;
	clrscr();
	printf("Enter a word: ");
	scanf("%s",&string1);

	orig(string1[],SIZE);



	 getch();
	 return 0;
}



	  char orig(char word,int size)
	  {
	   int x;
	      for(x=0;x<=)
		

			  //to use the the loop//???



:) :)

** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: reversing words using array in c language

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: reversing words using array in c language

Posted 18 February 2009 - 10:24 PM

Basically (this was done with C++ strings, easily convertible):

void reverse(string& s)
{
	 char* front, *back;
	 char* temp = new char[s.size()+1];
 
	 for(int i = s.size()-1, j = 0; i > 0; i--, j++)
	 {
		 //assignment
		 front = &s.at(j);
		 back = &s.at(i);
		 temp[i]= *front;
		 temp[j] = *back;
	 }
	 temp[s.size()] = '\0'; //append null terminator
	 //no nasty dangling pointers
	 front = 0;//not necessary since they terminate after each function wraps up
	 back = 0;//good to cover bases in case some expansion is planned for the program
 
	 //assign the reversed values to the string
	 for(int i = 0; i < s.size(); i++)
	 {
		s.at(i) = temp[i];
	 }
	 delete [] temp;
 }



You'll need to pass an a char* or char [], its size and replace appropriate names and you should be all set :)


Alternately (using less ptrs):

void switchit(string& s)
{
	   //allocate memory and get string values
	   //notice no additional pointers
	   char* temp = new char[s.size()+1];
	   for(int i = 0; i < s.size(); i++)
	   {
			   temp[i] = s.at(i);
	   }
	   temp[s.size()] = '\0';
 
	   char swap;
	   //swap it
	   for (int j = s.size()-1, i = 0; j > s.size()/2; j--, i++)
	   {
		   swap = temp[i];
		   temp[i] = temp[j];
		   temp[j] = swap;
	   }
	   //reassign switched to string reference
	   for(int i = 0; i < s.size(); i++)
	   {
			   s.at(i) = temp[i];
	   }
 
 
	   delete [] temp;
}




Still needs a quick conversion though.

This post has been edited by KYA: 18 February 2009 - 10:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1