Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to check whether two strings contain same characters in same order in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
     
void remove_duplicates(char *s) {
  for (int i = 0; i < strlen(s); i++) {
        for (int j = i + 1; s[j] != '\0'; j++) {
            if (s[j] == s[i]) {
                for (int k = j; s[k] != '\0'; k++) {
                    s[k] = s[k + 1];
                }
                j--;
            }
        }
    }
}
  
bool contain_same_characters_and_order(char s1[], char s2[]) {
    char *s1_tmp = (char *)malloc((strlen(s1) * sizeof(char)) + 1);
    char *s2_tmp = (char *)malloc((strlen(s2) * sizeof(char)) + 1);
      
    strcpy(s1_tmp, s1);
    strcpy(s2_tmp, s2);
     
    remove_duplicates(s1_tmp);
    remove_duplicates(s2_tmp);
     
	bool b = true;
    for (int i = 0; i < strlen(s1_tmp); i++) {
        if (s1_tmp[i] != s2_tmp[i]) {
			b = false;
			break;
        }
    }
     
    free(s1_tmp);
    free(s2_tmp);
	
    return b;
}
 
  
int main() {
    char s1[] = "c programming";
    char s2[] = "ccc proooogramminggggg";
       
    if (contain_same_characters_and_order(s1, s2))
        puts("yes");
    else
        puts("no");
}
    
    
     
/*
run:
     
yes
    
*/

 

 



answered Oct 29, 2019 by avibootz
edited Oct 29, 2019 by avibootz

Related questions

...