Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

New in C, swapping names

Status
Not open for further replies.

Johnston

Newbie level 1
Joined
Apr 9, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,287
I'm new in C and was hoping for a little bit help.

I need to write a program that reverses names to go from Gary Bronson to Bronson, Gary.

Any help is greatly appreciate. I've been searching the Internet for help for a while nlw
 

Hi Johnston,

I think below code should work for your requirement..



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
 
/* function to reverse a string */
 
void reverse(char *start, char *end);
 
/*Function to reverse words*/
 
void reversewords(char *s)
 
{
 
  char *word = s;
 
  char *temp = s;
 
  /*start reversing the individual words*/
 
  while( *temp )
 
  {
 
    temp++;
 
    if (*temp == '\0')
 
    {
      reverse(word, temp-1);
 
    }
 
    else if(*temp == ' ')
 
    {
 
      reverse(word, temp-1);
 
      word = temp+1;
 
    }
 
  } 
 
  reverse(s, temp-1);/*Reverse the resultant string from beginning to end including spaces*/
 
}
 
/*Function to reverse a string this is similar to earlier blog post to reverse a string */
 
void reverse(char *start, char *end)
 
{
 
  char temp;
 
  while (start < end)
 
  {
 
    temp = *start;
 
    *start++ = *end;
 
    *end-- = temp;
 
  }
 
}
 
int main()
 
{
 
  char s[] = "Neil Peart rocks YYZ";
 
  char *temp = s;
 
  printf("Original string: %s\n",s);
 
  reversewords(s);
 
  printf("Reverse word string: %s\n", s);
 
  return 0;
 
}

 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top