Sponsored Content
Full Discussion: String reverse
Top Forums Programming String reverse Post 302494868 by drl on Tuesday 8th of February 2011 05:16:24 PM
Old 02-08-2011
Hi.

From K & R, reverse in-place:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate K & R string reversal in-place.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

pl " Source:"
cat hi.c

pl " Compile, link, run results:"
rm -f hi
make hi
./hi

exit 0

producing:
Code:
% ./s1

-----
 Source:
#include <stdio.h>

int
main ()
{
  char s[] = "Hello, world.";
  void reverse ();
  printf ("%s\n", s);
  reverse (s);
  printf ("%s\n", s);
  return (0);
}

/* K & R, Page 62 */

#include <string.h>

void
reverse (char s[])
{
  char c;
  int i, j;
  for (i = 0, j = strlen (s) - 1; i < j; i++, j--)
    {
      c = s[i];
      s[i] = s[j];
      s[j] = c;
    }
}

-----
 Compile, link, run results:
cc     hi.c   -o hi
Hello, world.
.dlrow ,olleH

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

string in reverse

Can we print any string in reverse order? For example: oracle 16294 1 0 Aug 11 ? 0:00 ora_reco_crepd oracle 16276 1 0 Aug 11 ? 0:19 ora_dbw0_crepd I need second last column from this output. (0:00 & 0:19). I can use awk print $2 after reversing the string. ... (4 Replies)
Discussion started by: malaymaru
4 Replies

2. Shell Programming and Scripting

reverse string manipulation

How to get the reverse parsing work. I have a strings like aqw-wef-324-err.log wefd-324r-err.log efrt-4rfr.log . . i want to have string upto last hypen. aqw-wef-324 wefd-324r ... (1 Reply)
Discussion started by: senthilk615
1 Replies

3. Filesystems, Disks and Memory

shell program to reverse the string

pls help me in getting that program (1 Reply)
Discussion started by: saikiran
1 Replies

4. Shell Programming and Scripting

PerL Reverse the string.

Hi, I am very new to perl. My question: How i can reverse the given string using substr function but without using reverse function in perl? Anybody please help. thanks, -Lalit (3 Replies)
Discussion started by: email-lalit
3 Replies

5. Shell Programming and Scripting

reverse a string based on else

I have a file like this: Dog Cat One ABCDEFGHIJ house Dog Cat Two ABCDEFGHIJ house Cat Cat One ABCDEFGHIJ house Cat Cat Two ABCDEFGHIJ house I want to look at $3 and if it says "Two" print out the line except reverse $4. Dog Cat One ABCDEFGHIJ house Dog Cat Two JIHGFEDCBA house ... (3 Replies)
Discussion started by: dcfargo
3 Replies

6. Shell Programming and Scripting

reverse string matching

Guys, I am trying to find a way to achieve this. I need to print /usr/local/apche/htdocs only from the string /usr/local/apache/htdocs/file.php using the regex. The below did not work. I know a solution with normal cut, I need a way to do this with the awk regex. awk '/+file.php/' (6 Replies)
Discussion started by: anilcliff
6 Replies

7. Shell Programming and Scripting

Reverse of a string

Hi All, I have a String str="Manish". I would like to reverse it. I know the option to do this in bash is: echo "Manish" | rev but I have seen an alternate solution somewhere, which states that: str="Manish" echo $str | awk '{ for(i=length($0);i>=1;i--) printf("%s",substr($0,i,1));... (7 Replies)
Discussion started by: manishdivs
7 Replies

8. Shell Programming and Scripting

To reverse a string

Hi All, I would like to know , how to reverse a given string example : Hi how are you Required Output: you are how HiThanks (7 Replies)
Discussion started by: santhoshks
7 Replies

9. Shell Programming and Scripting

awk reverse string

Hello, Can anyone explain for me in this script to reverse the string? 1) the "x=x" part, how it works? $ echo welcome | awk '{ for(i=length;i!=0;i--)x=x substr($0,i,1);}END{print x}' $ emoclew2) x seems to be an array at the END, but can it automatically print the whole array in awk? Thanks... (8 Replies)
Discussion started by: yifangt
8 Replies

10. UNIX for Dummies Questions & Answers

How to cut part of a string in reverse?

Hi, how to cut part of a string sing delimiter in reverse input file 1,2,st-pa-tr-01,2,3,4, 2,3,ff-ht-05,6,7,8 how can i obtain strings till st-pa-tr ff-ht i.e cutting the last part og string -01 and -05 Thanks & Regards Nivi edit by bakunin: changed thread title (typo) (3 Replies)
Discussion started by: nivI
3 Replies
GEARMAN_EXECUTE(3)						     Gearmand							GEARMAN_EXECUTE(3)

NAME
gearman_execute - Gearmand Documentation, http://gearman.info/ SYNOPSIS
#include <libgearman/gearman.h> gearman_task_st *gearman_execute(gearman_client_st *client, const char *function_name, size_t function_name_length, const char *unique, size_t unique_length, gearman_work_t *workload, gearman_argument_t *arguments, void *context) gearman_task_st *gearman_execute_by_partition(gearman_client_st *client, const char *partition_function, const size_t partition_func- tion_length, const char *function_name, const size_t function_name_length, const char *unique_str, const size_t unique_length, gear- man_work_t *workload, gearman_argument_t *arguments, void *context) Link with -lgearman DESCRIPTION
gearman_execute() is used to create a new gearman_task_st that is executed against the function that is found via the function_name argu- ment. gearman_work_t can be used to describe the work that will be executed, it is built with gearman_argument_make(). The argument unique_str is optional, but if supplied it is used for coalescence by gearmand. gearman_argument_t is the work that the client will send the to the server If gearman_execute() is given a gearman_work_t that has been built with a reducer, it takes the gearman_argument_t and executs it against a function as it normally would, but it tells the function to then process the results through a reducer function that the gearman_work_t was created with. What is happening is that the function is mappping/splitting work up into units, and then sending each of them to the reducer function. Once all work is completed, the mapper function will aggregate the work via an aggregator function, gearman_aggregator_fn, and return a result. If any of the units of work error, the job will be aborted. The resulting value will be stored in the gearman_task_st. The result can be obtained from the task by calling gearman_task_result() to gain the gearman_result_st. RETURN VALUE
gearman_execute() returns a c:type:gearman_task_st. EXAMPLE
/* Example code to show how to send a string to a function called "reverse" and print the results. */ #include <string.h> #include <stdlib.h> #include <stdio.h> #include <libgearman/gearman.h> int main(void) { gearman_client_st *client= gearman_client_create(NULL); gearman_return_t ret= gearman_client_add_server(client, "localhost", 0); if (gearman_failed(ret)) { return EXIT_FAILURE; } gearman_argument_t value= gearman_argument_make(0, 0, "Reverse Me", strlen("Reverse Me")); gearman_task_st *task= gearman_execute(client, "reverse", strlen("reverse"), // function NULL, 0, // no unique value provided NULL, &value, 0); if (task == NULL) // If gearman_execute() can return NULL on error { fprintf(stderr, "Error: %s ", gearman_client_error(client)); gearman_client_free(client); return EXIT_FAILURE; } // Make sure the task was run successfully if (gearman_success(gearman_task_return(task))) { // Make use of value gearman_result_st *result= gearman_task_result(task); printf("%.*s ", (int)gearman_result_size(result), gearman_result_value(result)); } gearman_client_free(client); return EXIT_SUCCESS; } HOME
To find out more information please check: http://gearman.info/ SEE ALSO
gearmand(8) libgearman(3) AUTHOR
Data Differential http://www.datadifferential.com/ COPYRIGHT
2012, Data Differential, http://www.datadifferential.com/ 0.33 May 04, 2012 GEARMAN_EXECUTE(3)
All times are GMT -4. The time now is 05:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy