C code to Reverse words in a string

When it comes to C programming, the saying “Practice make perfect” is very much valid.
So here I am sharing a sample code to reverse words within a sample string.
Since this program does not belong specifically to the “Embedded C” category, this post is not marked under the category.

Working of this program is pretty simple 🙂

The pseudo-code is as follows…

I. Read-in each character within the string to an array until a ‘space’ character is found

II. When a ‘space’ character is found; print the earlier read string in reverse order

III. Repeat step I and II until the entire string is scanned

Note: The “flag” variable is used to mark whether the scan is presently within a word or outside of a word.

The output of the above program:

A Quick refresher on the CAN protocol

This article is intended to give you a quick refresher on the protocol. The following paragraphs are organized in such a way that the reader is expected to have a basic understanding of the CAN protocol.

The Controller Area Network (CAN) is a serial communications protocol which efficiently supports distributed realtime control with a very high level of security. Upto 1Mbps of communication speeds can be achieved in a CAN network.

CAN protocol with refernce to the OSI model:

1. The object layer:
The scope of the object layer includes finding which messages are to be transmitted, deciding which messages received by the transfer layer are actually to be used and providing an interface to the application layer related hardware.
2. The transfer layer:
The scope of the transfer layer mainly is the transfer protocol, i.e. controlling the framing, performing arbitration, error checking, error signalling and fault confinement. Also some general features of the bit timing are regarded as part of the transfer layer. It is in the nature of the transfer layer that there is no freedom for modifications.
3. The physical layer:
The scope of the physical layer is the actual transfer of the bits between the different nodes with respect to all electrical properties. Within one network the physical layer, of course, has to be the same for all nodes.

The CAN bus is a broadcast type of bus. There is no way to send a message to just a specific node; all nodes will invariably pick up all traffic.

Layered Structure of a CAN Node:

Basic Configuration:

The physical layer of CAN is a differential transmission on a twisted pair bus. The bus uses non-return-to-zero (NRZ) with bit stuffing. The nodes are connected to the network in a Wired-AND fashion.

A maximum of 8 Bytes can be transfered per message in CAN. The messages are protected by CRC type checksum. CAN bus access is handled via serial communication scheme of Carrier Sense Multiple Access/Collition Detection with Non-Destructive Arbitration.

There are no explicit address used in the CAN messages, instead each message carries a numeric value which contains its priority and also serve as an identification of the message.

CAN uses an elaborate error handling scheme that results in re-transmitted messages if they are not properly received. Also there are effective means for isolating faults and removing faulty nodes from the bus.

About the higher layer protocols using CAN:
Higher layer protocols such as Devicenet, CANKingdom, CANopen etc. are used to standardize network startup process, to distribute ‘addresses’ among participating nodes, to determine the layout of the messages, to provide routines for error handling at the system level. But the use of the higher layer protocols are entirely optional.

Message transfer is manifested and controlled by four different frame types:
1. DATA FRAME – Carries data from a transmitter to the receivers.
2. REMOTE FRAME – To request the transmission of the DATA FRAME with the same IDENTIFIER.
3. ERROR FRAME – Transmitted by any unit on detecting a bus error.
4. OVERLOAD FRAME – To provide for an extra delay between the preceding and the succeeding DATA or REMOTE FRAMEs.

When the bus is free any unit may start to transmit a message. The unit with the message of higher priority to be transmitted gains bus access. If 2 or more units start transmitting messages at the same time, the bus access conflict is resolved by bitwise arbitration using the IDENTIFIER. The mechanism of arbitration guarantees that neither information nor time is lost.

There are two different formats which differ in the length of the IDENTIFIER field: Frames with the number of 11 bit IDENTIFIER are denoted Standard Frames. In contrast, frames containing 29 bit IDENTIFIER are denoted Extended Frames.

CAN Standard Data Frame:

CAN Extended Data Frame: