
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print the longest prefix of the given string which is also the suffix of the same string in C Program.
Given a string in which we have to check that the length of the longest prefix which is also a suffix of the string like there is a string “abcab” so here “ab” is of length 2 and is the longest substring with same prefix and suffix.
Example
Input: str[] = { “aabbccdaabbcc” } Output: 6 Input: abdab Output: 2
If we will start the pointer from start and end of the string than they will get overlapped at some point so instead of doing that we will break the string from middle and start matching left and right string. If they are equal return size of any one of the matched string else try for shorter lengths on both the sides.
Algorithm
int longest(char str[], int n) START STEP 1 : DECLARE length AS 0 AND i AS n/2 STEP 2 : IF n < 2 THEN RETURN 1 STEP 3 :LOOP WHILE TILL str[i]!='\0' IF str[i] == str[length] THEN, INCREMENT length BY 1 INCREMENT i BY 1 ELSE IF length == 0 THEN, INCREMENT i BY 1 ELSE DECREMENT length BY 1 END IF END IF END WHILE RETURN length STOP
Example
#include <stdio.h> int longest(char str[], int n){ int length = 0, i = n/2; if( n < 2 ) return 1; while( str[i]!='\0' ){ //When we find the character like prefix in suffix, //we will move the length and i to count the length of the similar prefix and suffix if (str[i] == str[length]){ ++length; ++i; } else //When prefix and suffix not equal{ if(length == 0) ++i; else --length; } } return length; } int main(int argc, char const *argv[]){ char str[] = {"abccmmabcc"}; int n = sizeof(str)/sizeof(str[0]); int length = longest(str, n); printf("Length = %d", length); return 0; }
Output
If we run above program then it will generate following output:
Length = 4
- Related Articles
- Find the longest sub-string which is prefix, suffix and also present inside the string in Python
- Program to find longest prefix that is also a suffix in C++
- C++ Program to Find the Longest Prefix Matching of a Given Sequence
- Python Get the numeric prefix of given string
- First string from the given array whose reverse is also present in the same array in C++
- Print the string after the specified character has occurred given no. of times in C Program
- Check if suffix and prefix of a string are palindromes in Python
- Java program to print whether the given string is a palindrome
- Find length of longest subsequence of one string which is substring of another string in C++
- C Program to print all permutations of a given string
- Find the longest common prefix between two strings after performing swaps on second string in C++
- Print all the palindromic permutations of given string in alphabetic order in C++
- Program to print all substrings of a given string in C++
- How to find the length of the longest substring from the given string without repeating the characters using C#?
- C program to print the ASCII values in a string.

Advertisements