How would I write a C program to find the length of the longest substring of a given string without repeating characters?For example if given a string like "xyzxyzyytttt" how would my C program determine the length of the longest substring without repeating characters is 3? I've seen lots of tutorials for this in Java or C++, but I need to know how to do this in C. Thank you

Question
Asked Mar 22, 2019
32 views

How would I write a C program to find the length of the longest substring of a given string without repeating characters?

For example if given a string like "xyzxyzyytttt" how would my C program determine the length of the longest substring without repeating characters is 3? I've seen lots of tutorials for this in Java or C++, but I need to know how to do this in C. Thank you

check_circle

Expert Answer

Step 1

The given program is to find the maximum length of the substring without repeating character. This is determined by using the function "LongestSubstring()".

Step 2

Program:

// Include required header files
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define NOOFCHARS 256


// Declaration of minInt() function to get the minIntimum of two integers
int minInt(int a, int b);

/* Definition of LongestSubstring() function to
find maximum length Non-Repeating Character Substring */
int LongestSubstring(char *str)
{
// Declare and initialize required variables
int n = strlen(str);
int currLen = 1;
int maxLen= 1;
int prevIndx;
int i;

// Create of memory allocation
int *visited = (int *)malloc(sizeof(int)*NOOFCHARS );

// For loop to find character not visited
for (i = 0; i < NOOFCHARS ; i++)
visited[i] = -1;

// Assign 0 for visited character
visited[str[0]] = 0;

// For loop to start from second character
for (i = 1; i < n; i++)
{

prevIndx = visited[str[i]];

/* If the currentt character is not found in the
processed substring or it is not part of
the current maximum length Non-Repeating Character Substring */
if (prevIndx == -1 || i - currLen > prevIndx)

// Increment current length
currLen++;

// Otherwise
else
{
/* Check length of the
previous maximum length Non-Repeating Character Substring
was greater than maxLen or not */
if (currLen > maxLen)
maxLen= currLen;

currLen = i - prevIndx;
...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Is the following distributive law valid or invalid? Prove your answer. x XOR (y + z) = (x XOR y) + ...

A: Answer:Here, the truth table is used to get the answer for each side of the question.First take LHS,

question_answer

Q: Find a regular grammar that generates the language on Σ = {a, b} consisting of all strings with no m...

A: Regular grammar for the language Σ = {a, b} is as follows:

question_answer

Q: Write a PL/SQL block to accept a date from the user and print the day and the date as shown below. U...

A: Solution:Given data,The PL/SQL code read the user input of date and display the day and the date in ...

question_answer

Q: Write a PL/SQL block to retrieve and display the students enrolled in a course.

A: Click to see the answer

question_answer

Q: Not very familiar with RAID so I would really appreciate some help and an explanation behind the ans...

A: RAID is an acronym for Redundant Array of Independent Disk. It is a process of storing the data into...

question_answer

Q: Using adders, subtracters, comparers, copiers, or multipliers, draw block diagrams for Turing machin...

A: Solution:Turing machine:The Turing machine is theoretical computing machine that can be used to iden...

question_answer

Q: What form of storage doses a password manger store encrypted passwords? Is it local, web sites, clod...

A: Passwords managers allows one to generate strong and unique passwords and store them in one safe pla...

question_answer

Q: need help with understanding how to do the SQL code for the following question:  These are the heade...

A: As the table name is not shown in the picture, we are assuming the table name as Product. There are ...

question_answer

Q: Show that the following grammar is ambiguous: S → aSbS |bSaS| λ.

A: Solution:Given that,Proof: Given grammar is ambiguous.