2021年3月5日星期五

What is this code doing. Check two strings for anagrams

This code checks if two strings for an anagram. Can you please explain how it works what is it even doing.

#include <stdio.h>    int t[256],i;  int main(int c)  {      for(;c+3;)          (i=getchar())>10?t[i]+=c:(c-=2);        for(i=257;--i&&!t[i-1];);      puts(i?"false":"true");      }  

Here's what I've understood while poking at the source code: Its maintaining an array t[], and storing values at index = character's ascii value. The value stored is 1 for the first string's characters and -1 for the second string's characters. If a character is in both the strings, the value stored in it is zero. 1 + (-1). The first for loop is the input.

can you explain what this loop is checking, and how does it set up the value of variable i : for(i=257;--i&&!t[i-1];);

Code from: https://developerinsider.co/find-anagram-string-programming-puzzles/

https://stackoverflow.com/questions/66501487/what-is-this-code-doing-check-two-strings-for-anagrams March 06, 2021 at 09:21AM

没有评论:

发表评论