- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print shortest path to print a string on screen in C Program.
Given a string, the program must display the shortest path which will print the string over the screen using that shortest path.
Like screen will store alphabets in the format
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Example
Input: HUP Output : Move Down Move Down Move Down destination reached Move Left Move Left Move Down Move Down Move Down destination reached Move Up destination reached
The approach used here is to store the characters in the n x n matrix and perform the following operation −
If row difference is negative then move up If row difference is positive then move down If column difference is negative then go left If column difference is positive then we go right
Algorithm
START Step 1 -> Declare Function void printpath(char str[]) Declare variable int i = 0 and cx=0 and cy=0 Loop While str[i] != '\0' Declare variable as int n1 = (str[i] - 'A') / 5 Declare variable as int n2 = (str[i] - 'B' + 1) % 5 Loop while cx > n1 Print move up cx— End Loop while cy > n2 Print Move Left Cy— End Loop while cx < n1 Print move down Cx++ End Loop while cy < n2 Print move down Cy++ End Print destination reached I++ Step 2 -> in main() Declare char str[] = {"HUP"} Call printpath(str) STOP
Example
#include <stdio.h> void printpath(char str[]){ int i = 0; // start from character 'A' present at position (0, 0) int cx = 0, cy = 0; while (str[i] != '\0'){ // find cordinates of next character int n1 = (str[i] - 'A') / 5; int n2 = (str[i] - 'B' + 1) % 5; // Move Up if destination is above while (cx > n1){ printf("Move Up
"); cx--; } // Move Left if destination is to the left while (cy > n2){ printf("Move Left
"); cy--; } // Move down if destination is below while (cx < n1){ printf("Move Down
"); cx++; } // Move Right if destination is to the right while (cy < n2){ printf("Move Down
"); cy++; } // At this point, destination is reached printf("destination reached
"); i++; } } int main(int argc, char const *argv[]){ char str[] = {"HUP"}; printpath(str); return 0; }
Output
If we run the above program then it will generate the following output −
Move Down Move Down Move Down destination reached Move Left Move Left Move Down Move Down Move Down destination reached Move Up destination reached
- Related Articles
- Program to print the first shortest root to leaf path in a Binary Tree using C++
- Print the first shortest root to leaf path in a Binary Tree in C++ Programming.
- C program to print string tokens
- Java Program to Print a String
- Kotlin Program to Print a String
- C program to print the ASCII values in a string.
- C program to print a string without any quote in the program
- How to Print a String in Swift Program?
- Program to print all substrings of a given string in C++
- C Program to print all permutations of a given string
- Program to print the longest leaf to leaf path in a Binary tree using C++
- Program to print Inverse Diamond pattern on C++
- Program to print path from root to a given node in a binary tree using C++
- Print common nodes on path from root (or common ancestors) in C++
- C++ Program for Dijkstra’s shortest path algorithm?

Advertisements