C Programming: Length of longest common subsequence of two strings

C String: Exercise-41 with Solution

Write a C program to calculate the length of the longest common subsequence of two given strings. The strings consist of alphabetical characters..

Sample Data:

("abcdkiou", "cabsdf") -> 3
("pqrjad", "qr") -> 2

Sample Solution:

C Code:

#include <stdio.h>
#include <string.h>

#define max(x,y)((x)>(y)?(x):(y))
int result[200][200];
char str1[200], str2[200];
int main(){
	printf("Input the first string: ");
	printf("\nInput the second string: ");
	int n=strlen(str1);
	int m=strlen(str2);
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
      printf("\nLength of longest common subsequence of said strings: %d\n",result[n][m]);

Sample Output:

Input the first string: 
Input the second string: 
Length of longest common subsequence of said strings: 3

Flowchart :

Flowchart: Length of longest common subsequence of two strings.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous C Programming Exercise: Replace every lowercase letter with the same uppercase.
Next C Programming Exercise: C Date Time Exercises Home

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

Where in memory variables stored in C:

You got some of these right, but whoever wrote the questions tricked you on at least one question:

  • global variables -------> data (correct)
  • static variables -------> data (correct)
  • constant data types -----> code and/or data. Consider string literals for a situation when a constant itself would be stored in the data segment, and references to it would be embedded in the code
  • local variables(declared and defined in functions) --------> stack (correct)
  • variables declared and defined in main function -----> heap also stack (the teacher was trying to trick you)
  • pointers(ex: char *arr, int *arr) -------> heap data or stack, depending on the context. C lets you declare a global or a static pointer, in which case the pointer itself would end up in the data segment.
  • dynamically allocated space(using malloc, calloc, realloc) --------> stack heap

It is worth mentioning that "stack" is officially called "automatic storage class".

Ref : https://bit.ly/3jNvuZh

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook